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!

5
  • 1
    Use a List<Student> instead of an array - then you don't have a fixed length Commented Sep 18, 2015 at 13:45
  • 1
    Or check if students[i] != null before checking the FirstName Commented Sep 18, 2015 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 Commented Sep 18, 2015 at 13:48
  • 3
    possible duplicate of What is a NullReferenceException and how do I fix it? Commented Sep 18, 2015 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. Commented Sep 18, 2015 at 14:09

2 Answers 2

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?

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

1 Comment

oh so simple... Thank You!
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
}

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.