代码适用于一种情况,但不适用于其他情况

Code works for one case but not other cases

我通过声明了 4 个对象数组;

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();

然后我做了一些大小写切换语句,要求输入如下名称:

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);
}

但是当我尝试在其他对象上输入名称时,例如:

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.

是的,基础 class(称为 "Student")具有所有 "Name" setter 和 getter,而派生的 class es BSIT 和 BSCS 都只是从 class Student.

获得 "Name" 字段

为什么当我为 BSIT 输入信息时程序运行正常,但为 BSCS、MSIT 和 MSCS 输入信息却没有? MSIT 和 MSCS 遵循与 BSIT 和 BSCS 完全相同的格式,但它们仍然不起作用。

发生这种情况是因为在您的初始化代码中,您在索引 studCountiden:

处初始化了对象
Student[,] s2 =new BSIT[10,4];
s2[studCount,iden] = new BSIT();

所以这对于情况 1 工作正常,因为位置 studCount,iden 处的元素已初始化。

但是,对于情况 2,您正在执行此代码:

iden = 1;

因此,您访问的不是已初始化的 s2[studCount,0],而是未初始化的 s2[studCount,1](即 s2[studCount,1] == null

修复它的一种方法是在访问数组的任何元素之前,您可以验证它是否不为 null 然后才使用它:

iden = 1;
Console.Write("Enter Name: ");
if(s3[studCount,iden] == null)
    s3[studCount,iden] = new BSCS();
(s3[studCount,iden]).setName(Console.ReadLine());       

或者在创建数组后立即初始化所有数组项。

问题:

您只初始化了 1 个元素

s2[studCount,iden] = new BSIT();
//and so on

我想是 0,所以只要您访问索引 [studCount,0] 上的元素,它就可以正常工作。但不是其他元素。

解法:

iden = 1;
Console.Write("Enter Name: ");
s3[studCount,iden] = new BSCS(); //just initialize it before use
(s3[studCount,iden]).setName(Console.ReadLine());     

您的情况:

你必须根据他们的Discipline保存Students。这就是为什么你生成了一个包含 4 列(BSIT、BSCS、MSIT、MSCS)的数组,但你没有像你应该的那样使用它们。您创建了 4 个具有 4 列的数组,现在使用您的方法,您的数组将在最后的某些位置包含空元素。

你的方法应该是:

你class结构是:

Student
    BSIT
    BSCS
    MSIT
    MSCS

所以只需创建一个 Student 的数组并在其中放入不同的子 class 个实例。

Student[,] students = new Student[10,4]; //index 0 is for BSIT, 1 for BSCS and so on..

我只是在这里为 2 个案例编码,以帮助您了解工作原理:

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...

现在什么都不会覆盖,所有学科都将在其指定的列中。

注意:我已经更改了数组的名称,所以请在您的代码中修改它