Reputation: 27
I declared 4 arrays of objects via;
Student[,] s2 =new BSIT[10,4];
s2[studCount,iden] = new BSIT();
Student[,] s3 = new BSCS[10,4];
s3[studCount,iden] = new BSCS();
Student[,] s4 = new MSIT[10,4];
s4[studCount,iden] = new MSIT();
Student[,] s5 = new MSCS[10,4];
s5[studCount,iden] = new MSCS();
And then I made some case switch statements that ask for names like:
case 1://BSIT
{
iden=0;
Console.Write("Enter Name: ");
(s2[studCount,iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());
if(a>=15 && a<=30)
{
(s2[studCount,iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}
But when I tried inputting just the name on other other objects like:
case 2://BSCS
{
iden = 1;
Console.Write("Enter Name: ");
(s3[studCount,iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());
if(a>=15 && a<=30)
{
(s3[studCount,iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}
It gives me the System.NullReferenceException: Object Reference not set to instance of object.
Yes the base class (called "Student") is has all the "Name" setter and getter, while the derived classes BSIT and BSCS both just get the "Name" field from class Student.
How come the program works fine when I input info for BSIT, but not for BSCS, MSIT, and MSCS? MSIT and MSCS follow the exact same format as BSIT and BSCS, but they still don't work.
Upvotes: 1
Views: 91
Reputation: 12439
Problem:
You're only initializing 1 element
s2[studCount,iden] = new BSIT();
//and so on
Which is 0
I suppose, so whenever you access the element on index [studCount,0]
it works fine. But not other elements.
Solution:
iden = 1;
Console.Write("Enter Name: ");
s3[studCount,iden] = new BSCS(); //just initialize it before use
(s3[studCount,iden]).setName(Console.ReadLine());
Your Scenario:
You have to save Student
s according to their Discipline
. Thats why you generated an array with 4 columns (BSIT, BSCS, MSIT, MSCS) but you're not using them like you should. You created 4 arrays with 4 columns, now with your approach, your arrays will have empty elements some where in the end.
Your approach should be:
You class structure is:
Student
BSIT
BSCS
MSIT
MSCS
So just create an array of Student
and put different child class instances in it.
Student[,] students = new Student[10,4]; //index 0 is for BSIT, 1 for BSCS and so on..
I am just coding for 2 cases here to help you understand the working:
case 1://BSIT
{
iden = 0;
Console.Write("Enter Name: ");
//here you'll initialize the object
students[studCount, iden] = new BSIT(); //Student is base, so it can contain all of it's child objects
(students[studCount, iden]).setName(Console.ReadLine());
do
{
Console.Write("Enter Age(15-30 only): ");
int a = int.Parse(Console.ReadLine());
if(a>=15 && a<=30)
{
(students[studCount, iden]).setAge(a);
valid=1;
}
else
{
valid=0;
Console.WriteLine("INVALID INPUT. TRY AGAIN.");
}
}
while(valid==0);
}
case 2://BSCS
{
iden = 1;
Console.Write("Enter Name: ");
(s3[studCount,iden]).setName(Console.ReadLine());
iden = 1;
Console.Write("Enter Name: ");
//here you'll initialize the object
students[studCount, iden] = new BSCS(); //Student is base, so it can contain all of it's child objects (here we're initializing BSCS)
(students[studCount, iden]).setName(Console.ReadLine());
//your code...
Now nothing will override, and all disciplines will be at their specified columns.
Note: I have changed array's name, so please fix it in your code everywhere
Upvotes: 3
Reputation: 24901
This happens because in your initialization code you initialize object at indexes studCount
and iden
:
Student[,] s2 =new BSIT[10,4];
s2[studCount,iden] = new BSIT();
So this works fine for case 1, because element at location studCount,iden
is initialized.
However, for case 2 you are performing this code:
iden = 1;
So instead of accessing s2[studCount,0]
, which is initialized, you are accessing s2[studCount,1]
, which is not initialized (i.e. s2[studCount,1] == null
)
One way to fix it is before accessing any element of an array, you can verify if it is not null and only then use it:
iden = 1;
Console.Write("Enter Name: ");
if(s3[studCount,iden] == null)
s3[studCount,iden] = new BSCS();
(s3[studCount,iden]).setName(Console.ReadLine());
Or initialize all array items immediately after you create an array.
Upvotes: 2