0

Ok, so I'm trying to do this task I got some classes and first of all I want to make them and make their print functions. Later I'll need to make and "addStudent" function to the group class, so i want my array to be bigger than needed.

so here's my code

static void Main(string[] args)
    {
        Student test = new Student("as", "dsa");
        Student test1 = new Student("Stola", "Stolova");
        Student test2 = new Student("alo", "maloo");
        Student[] students = new Student[5];
        students[0] = test;
        students[1] = test1;
        students[2] = test2;           
        Group grp = new Group("aklas", students);
        grp.print();
        Console.Read();
    }
//^
//this is my main function

class Student
{
    private string firstName;
    private string lastName;
    public string FirstName
    {
        get
        {
            return firstName;
        }
    }
    public Student()
    {
        firstName = null;
        lastName = null;
    }
    public Student(string nameOne, string nameTwo)
    {
        firstName = nameOne;
        lastName = nameTwo;
    }
    public void formatPrint()
    {
        Console.WriteLine("{0,10} {1,10}", firstName, lastName);
    }

// ^this is one of the classes

class Group
{
    private string name;
    private Student[] students;
    public Group()
    {
        name = null;
        students = null;
    }
    public Group(string grpName, Student[] grpStudents)
    {
        name = grpName;
        students = grpStudents;
    }
    public void print()
    {
        Console.WriteLine("{0, 15}", name);
        for(int i = 0; i < students.Length; i ++)
        {
            if (students[i].FirstName != null)
                students[i].formatPrint();
        }
    }
}

//^ and here is the other one.

So the problem is i got array declared for 5 elements and there are only 3 elements inside. everything works fine if i declare the array with 3 elements, but if they are 4 or more im getting that null exeption even that i made that if statement. So where is my mistake? Thank you!

  • 1
    Use a `List` instead of an array - then you don't have a fixed length – Hans Kesting Sep 18 '15 at 13:45
  • 1
    Or check if `students[i] != null` before checking the `FirstName` – Hans Kesting Sep 18 '15 at 13:46
  • 1
    You have an array length of 5 when you have added only 3 students . Like Hans said use a list of student this will allow for a dynamicly growing array – TinMan7757 Sep 18 '15 at 13:48
  • 3
    possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ron Beyer Sep 18 '15 at 13:51
  • You can replace students.Count by students.Count(s => s != null) but beware this will only work if there are no empty elements at the beginning or inbetween. – Bgl86 Sep 18 '15 at 14:09

2 Answers2

2

You are invoking a property FirstName of null object. This is the cause of NullReferenceException.

You should check if object is null:

if(students[i] != null)
   students[i].formatPrint();

Also check this question: What is a NullReferenceException and how do I fix it?

Kędrzu
  • 2,385
  • 13
  • 22
1

Dont use use array use genereic List.This will dynamically increase your class without worrying about size of array.

List<student> cl=new List<student>();
class.add(newstudent);
//to print all  students in class use
foreach(Student r in cl)
{
// print info of students
}
Raj
  • 28
  • 7