Click here to Skip to main content
15,063,287 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i tried this below code but its showing error Like system.nullreference Exception object not set to......etc..

What I have tried:

C#
using System;
class _8__CreatingObjectAsArray
{
   static int i;
    static void Main(string[] args)
    {
        students[] s = new students[5];
        //for (int i = 0; i < s.Lengts; i++)
        //    s[i] = new students[5];
        Console.WriteLine(s.Length);
        for (i = 0; i < s.Length; i++)
            s[i].getvals();
        
        for (i = 0; i < s.Length; i++)
            s[i].display();
    }
}
class students
{
    private int rno, marks;
    private string sname;
    public void getvals()
    {
        Console.WriteLine("enter roll no of student...");
        rno = int.Parse(Console.ReadLine());
        Console.WriteLine("enter name of student...");
        sname = Console.ReadLine();
        Console.WriteLine("enter marks of student...");
        marks = int.Parse(Console.ReadLine());
    }
    public void display()
    {
        Console.WriteLine("roll no = "+rno+"stu name = "+sname+"marks = "+marks);
    }
}
Posted
Updated 3-Mar-16 7:42am
v2

On this line:
C#
s[i].getvals();

..you're attempting to access a students-object in the array but there is none yet. The array at this point contains nothing but nulls, so you're getting the null-reference exception here.

I assume the for-loop which you've commented out should look like this:
C#
for (int i = 0; i < s.Length; i++)
       s[i] = new students();
(Which then will assign a new students-object to each array-index.)

Sidenote: In C#, class names and method names should be "CamelCase" and as one object of students represents one student, I suggest to name the class Student instead. And the methods: Display and GetVals or GetValues.
   
v3
Comments
Maciej Los 3-Mar-16 14:32pm
   
5ed!
Sascha Lefèvre 3-Mar-16 14:39pm
   
Thank you Maciej.
Hi, the problem is, you just created an array for holding objects of type Students, you didn't assign student objects to that array, the array is empty. so just instantiate the array by assigning student object. change the code as follows inside main,
C#
students[] s = new students[5];
s[0] = new students();
s[1] = new students();
s[2] = new students();
s[3] = new students();
s[4] = new students();
//for (int i = 0; i < s.Lengts; i++)
//    s[i] = new students[5];
Console.WriteLine(s.Length);
for (i = 0; i < s.Length; i++)
    s[i].getvals();

for (i = 0; i < s.Length; i++)
    s[i].display();

Console.ReadLine();
   
Comments
Sascha Lefèvre 3-Mar-16 14:41pm
   
Basically right but a bit inefficient to not use a for-loop for the array initialization ;) +4
VR Karthikeyan 3-Mar-16 23:07pm
   
thanks, you are right.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900