0

I got null exception error while executing this program. Although I have initialize the array

public class student
{
    string name;

    public string _name
    {
        set
        {

            this.name = value;
        }
        get
        {
            return name;
        }
    }
    public int _marks{  set; get; }
}

student[] arr = new student[3];

arr[0] = new student();
arr[0]._name = "AB";
arr[0]._marks = 44;

arr[1] = new student();
arr[1]._name = "amla";
arr[1]._marks = 75;


foreach (student i in arr)
{
    Console.WriteLine("\nname:{0}\nmarks:{1}", i._name, i._marks);
}
Console.Read();
4
  • 1
    Where do you get the exception? Commented Feb 3, 2015 at 3:18
  • after compiling it shows the output(out put is correct) but it gives the exception handle also. Commented Feb 3, 2015 at 3:20
  • That doesn't tell me where the exception occurs. You need to learn how to figure out which line in your code is throwing an exception. Commented Feb 3, 2015 at 3:23
  • 1
    You're not instantiating all elements, but you're iterating over the entire array. Commented Feb 3, 2015 at 3:55

5 Answers 5

1

Although you haven't done a good job of indicating where exactly the error is occurring, the code you've posted initialized arr[0] and arr[1], but never initializes arr[2].

Since you allocated an array with three elements, all three of them are accessed from your loop at the end.

Therefore, I would expect a null reference exception when you attempt to access the properties of this last element.

Sign up to request clarification or add additional context in comments.

1 Comment

I got the array on the following line Console.WriteLine("\nname:{0}\nmarks:{1}", i._name, i._marks); yeah I got it now I have to change the size of the array
1

Your array size is 3, but you have initialized only 2 elements to the array. While there are elements present in arr[0] and arr[1], arr[2] returns null reference as you have no data initialized there. You can add data to arr[2] or change the array size to 2 using

student[] arr = new student[2]

Comments

1

You are getting the NullReferenceException because you're attempting to access the properties of a null student in your loop (you have only initialized the first two, but not the last one).

One way to avoid this exception is to do a null check in your loop, so if you haven't initialized some of the items, you can still output the names of the students who have been initialized:

foreach (student i in arr.Where(s => s != null))
{
    Console.WriteLine("\nname:{0}\nmarks:{1}", i._name, i._marks);
}

You could also only show students who actually have a name (by checking for a null name) like:

foreach (student i in arr.Where(s => s != null && s._name != null))
{
    Console.WriteLine("\nname:{0}\nmarks:{1}", i._name, i._marks);
}

But really something like this should probably be done in the student class itself, so the client doesn't have to. You might consider setting some default value for name in the student class, so if it isn't set you can still display something (like "No Name"), and you can also overwrite the ToString() method so that the student class knows how to display itself properly. And, while you're at it, classes and public properties are typically PascalCase in C#:

public class Student
{
    private string _name;

    public string Name
    { 
        get { return _name; } 
        set { if (value != null) _name = value; }
    }

    public int Marks { get; set; }

    public Student()
    {
        Name = "<No Name>";
    }

    public override string ToString()
    {
        return string.Format("{3}Name:{0}{3}Marks:{1}", 
            Name, Marks, Environment.NewLine));
    }
}

With this in place, you can then do something simpler like:

foreach (student i in arr.Where(s => s != null))
{
    Console.WriteLine(i);
}

Comments

0

Try changing the size of the array:

student[] arr = new student[2];

Comments

0

You have an array with the size of 3, but you only add 2 elements.

Change

student[] arr = new student[3];

To

student[] arr = new student[2];

This happens because you try to access members of the student class when it hasn't been initialized.

You could do as I said above or add a 3rd student.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.