-1

I always have coded in C++ but I am learning C# now. I got a trouble with accessing the object value.

Here is what I tried.

class Program
{         
    static void Main(string[] args)
    {
        StudentInfo[] student = new StudentInfo[2];

        student[0] = new StudentInfo(100, 4);
        student[1] = new StudentInfo(101, 3);

        Trying ty = new Trying();
        ty.trial(); /// I got trouble with this line
    }
}
class Trying
{
    StudentInfo[] student = new StudentInfo[2]; // I added this line since it prevents compiler error
    public void trial()
    {   
        Console.WriteLine(student[1].Gpa); // I got trouble with this line
    }
}
class StudentInfo
{        
    public int studentNo {get; set;}
    public int Gpa {get; set;}
    public StudentInfo(int cc, int ct)
    {
        studentNo = cc;
        Gpa = ct;
    }
}

The error says

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.

How can I access the value student[1].Gpa correctly in the class Trying?

8
  • FYI, the line you added to prevent the compiler error is just creating another array (not initialized and not linked to the one in Main function). Read about variables (and fields, and properties) scope. EDIT: not that there are many similarities with C++. "Equivalent" code would be quite invalid in C++ as well.
    – Pac0
    Commented Mar 15 at 23:33
  • To solve the issue, we would need to know what do you intend to print exactly, what is the goal ?
    – Pac0
    Commented Mar 15 at 23:37
  • I expected to output "3" since the Gpa for student[1] is 3. Commented Mar 15 at 23:45
  • @EuijinJung you need to learn about variable scope
    – T.S.
    Commented Mar 15 at 23:59
  • 1
    I should work on learning about variable scope from now on. I didn't know that there were lots of differences between C++ and C#. It is tricky but I'll get used to it. Thanks for helping me today. Commented Mar 16 at 0:06

3 Answers 3

2

In your Trying class you just created an array named student of type StudentInfo then with trial() method you tried to access element at index 1 of the student array which is null by default because you have not added any element to your array.

here is my solution: in your trial method you can get two arguments like this:

public void trial(StudentInfo std1, StudentInfo std2)
{
    student[0] = std1;
    student[1] = std2;
    Console.WriteLine(student[1].Gpa); 
}

and then in your Main method: you can pass two object from your StudentInfo class to the trial() method:

static void Main(string[] args)
{
    StudentInfo[] student = new StudentInfo[2];
    
    StudentInfo std1 = new StudentInfo(100, 4);
    StudentInfo std2 = new StudentInfo(101, 3);

    Trying ty = new Trying();
    ty.trial(std1,std2); 
}

This way you can achieve the result you want.

4
  • you turned it into something else.
    – T.S.
    Commented Mar 15 at 23:46
  • How can I access the value "student[1].Gpa" correctly in the class Trying? doesn't my solution answers this question? @T.S.
    – Hr.Panahi
    Commented Mar 15 at 23:47
  • your answer focuses on refactory instead of explaining why code is not working
    – T.S.
    Commented Mar 15 at 23:49
  • @T.S. I explained why his code throws exception and then wrote my solution.
    – Hr.Panahi
    Commented Mar 15 at 23:50
2

The student in your main is NOT connected to the student in your Trying. You can disregard student code in your main. In Trying You did initialize an array but not added any objects. Array of nulls. See in comments how to fix it

class Trying
{
    StudentInfo[] students = new StudentInfo[2]; // <-- init array of nulls 

    // option 1 - init in constructor
    public Trying()
    {
        // option 1
        students[0] = new StudentInfo(1, 1); 
        students[1] = new StudentInfo(2, 2);
        students[2] = new StudentInfo(3, 3);
    }

    

    // option 2 - init in class declatrations
    StudentInfo[] students = new StudentInfo[] 
    {
        new StudentInfo(1, 1),
        new StudentInfo(2, 2),
        new StudentInfo(3, 3)
    };
  
    public void trial()
    {   
        Console.WriteLine(student[1].Gpa); // I got trouble with this line
    }
}

Ideal code

class Trying
{
    private StudentInfo[] students = new StudentInfo[] 
    {
        new StudentInfo(1, 1),
        new StudentInfo(2, 2),
        new StudentInfo(3, 3)
    };
  
    public void trial()
    {   
        Console.WriteLine(students[1].Gpa); // I got trouble with this line
    }
}
1
  • 1
    Slight mistake there - it should be: Console.WriteLine(students[1].Gpa);
    – Alex
    Commented Mar 15 at 23:53
1

The problem here is that you are indeed creating a StudentInfo array, but you are not passing it into the Trying class. The StudentInfo array you create inside the Trying class does solve the compiler error, but it is composed of object with no initialized values. I think a good approach would be to create a constructor for the Trying class into which you can pass your StudentInfo array, so that it contains the values you set in the Program Main method. Here you have an example:

class Program
{
    static void Main(string[] args)
    {
        StudentInfo[] students = new StudentInfo[2]; //<-- changed student to students

        students[0] = new StudentInfo(100, 4);
        students[1] = new StudentInfo(101, 3);

        Trying ty = new Trying(students); //<-- Here you pass the array from above
        ty.trial(); /// I got trouble with this line
    }
}
class Trying
{
    StudentInfo[] students; //<-- Also added the plural, the "s", since it is a colletion of objects
    public Trying(StudentInfo[] students)
    {
        this.students = students; //<-- Here the passed array is used to initialize the students field of the trying class
    }

    public void trial()
    {
        Console.WriteLine(students[1].Gpa); //<-- Now the second student is no longer null, same with its Gpa field
    }
}
class StudentInfo
{
    public int studentNo { get; set; }
    public int Gpa { get; set; }
    public StudentInfo(int cc, int ct)
    {
        studentNo = cc;
        Gpa = ct;
    }
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.