2016-04-27 124 views
-2

原谅一个愚蠢的问题,但我是新来的C#& OOP。如何初始化嵌套数组

任何人都可以帮助我防止“System.NullReferenceException:”?

我正在尝试按照指示完成一项任务,并使用我们迄今为止学到的内容(数组,类和构造函数)。

我创建了一个StudentSubjects类的数组,并将其嵌入到一个Student类的数组中。

我想打印每个学生的科目的详细信息。

我可以访问StudentArray[0]领域OK,但因为“对象引用不设置到对象的实例”不能获取到StudentArray[0].StudentSubjectsArray[0]领域

我已经待了两个星期寻找一个答案,但无法找到如何设置

StudentArray[0].StudentSubjectsArray[0].SubjectName = "Algebra"; 

任何意见,任何例子最欣赏的。我的代码如下....

using System; 

namespace Nested_Arrays 
{ 
    public class Program 
    { 
     static void Main(string[] args) 
     { 
      Student[] StudentArray = new Student[1]; 

      Console.WriteLine($"Hello"); 
      StudentArray[0] = new Student(); 
      StudentArray[0].StudentName = "Peter"; 
      StudentArray[0].StudentLocation = "Australia"; 
      Console.WriteLine($"{StudentArray[0].StudentName,10} {StudentArray[0].StudentLocation,15}"); 

      StudentArray[0].StudentSubjectsArray[0].SubjectName = "Algebra"; 
      StudentArray[0].StudentSubjectsArray[0].StudentsResult = "Pass"; 
      Console.WriteLine($"{StudentArray[0].StudentName,10} {StudentArray[0].StudentLocation,15} {StudentArray[0].StudentSubjectsArray[0].SubjectName,15} {StudentArray[0].StudentSubjectsArray[0].StudentsResult,10}"); 
      Console.WriteLine($"Goodbye"); 
     } 

     public class Student 
     { 
      public string StudentName; 
      private string studentName 
      { get { return studentName; } set { studentName = value; } } 

      public string StudentLocation; 
      private string studentLocation 
      { get { return studentLocation; } set { studentLocation = value; } } 

      public StudentSubjects[] StudentSubjectsArray; 
      private StudentSubjects[] studentSubjectsArray 
      { get { return studentSubjectsArray; } set { studentSubjectsArray = value; } } 

      //Constructor 
      public Student() { } 
     } 

     public class StudentSubjects 
     { 
      public string SubjectName; 
      private string subjectName 
      { get { return subjectName; } set { subjectName = value; } } 

      public string StudentsResult; 
      private string studentsResult 
      { get { return studentsResult; } set { studentsResult = value; } } 

      //Constructor 
      public StudentSubjects() { } 

     } 
    } 
} 
+0

你的代码看起来很奇怪。我想建议使用'List'而不是数组 - 它更灵活和安全地处理集合。 – MaKCbIMKo

+0

你好MaKCbIMKo。我不能使用列表,因为这是一个任务,我必须做老师所说的 - 他说使用数组。 – Joe

回答

3

你只需要添加以下内容:

StudentArray[0].StudentSubjectsArray = new StudentSubjects[1]; 
StudentArray[0].StudentSubjectsArray[0] = new StudentSubjects(); 
// and only then 
StudentArray[0].StudentSubjectsArray[0].SubjectName = "Algebra"; 
+0

不,OP需要先将一个数组分配给'StudentSubjectsArray'。 – juharr

+0

@juharr - 这个答案就是这样。你错过了什么,或者我? – AgapwIesu

+0

我是显然缺少基本阅读理解的人。 – juharr

0

正如其他人所说,你需要你在阵列中分配对象之前创建数组,虽然可以在同一时间两者都做。

例如,如果你想要做的这一切在同一行,这会工作:

Student[] StudentArray = { 
    new Student { 
     StudentName = "Peter", 
     StudentLocation = "Australia", 
     StudentSubjectsArray = new[] { 
      new StudentSubjects { 
       SubjectName = "Algebra", 
       StudentsResult = "Pass" 
      } 
     } 
    } 
}; 
+0

谢谢加布里埃尔,它确实有效。它看起来像一个有用的速记。经过两个星期,我的头撞墙,我永远不会拿出任何像你的建议。 – Joe

0

在我看来,一个好的做法是在构造函数初始化数组。通过这种方式,您可以确定在使用该对象时它不为空。

所以做这样的事情:

//Constructor 
public Student() { 
    this.studentSubjectsArray = new StudentSubjects[1]; 
} 
+0

谢谢卡多加我会尝试。 – Joe