本文介绍了C#(对象数组)对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原谅一个愚蠢的问题,但我是C#& OOP。



任何人都可以帮我阻止System.NullReferenceException:吗?



我是尝试按照说明进行作业并使用我们迄今为止学到的东西(数组,类和构造函数)。



我创建了一系列StudentSubjects类和将其嵌入到一系列学生课程中。



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



我可以访问StudentArray [0]字段,但无法访问StudentArray [0] .StudentSubjectsArray [0]字段,因为对象引用未设置为对象的实例



我花了2个星期寻找答案但找不到任何如何设置的例子



StudentArray [0] .StudentSubjectsArray [ 0] .SubjectName =代数;



最值得赞赏的建议。



什么我试过了:



使用系统;

命名空间 Nested_Arrays
{
public class 程序
{
静态 void Main( string [] args)
{
Student [] StudentArray = new 学生[ 1 ];

Console.WriteLine($ Hello);
StudentArray [ 0 ] = new Student();
StudentArray [ 0 ]。StudentName = 彼得;
StudentArray [ 0 ]。StudentLocation = 澳大利亚;
Console.WriteLine($ {StudentArray [0] .StudentName,10} {StudentArray [0]。 StudentLocation,15});

StudentArray [ 0 ]。StudentSubjectsArray [ 0 ]。SubjectName = 代数;
StudentArray [ 0 ]。StudentSubjectsArray [ 0 ]。StudentssResult = 传递;
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 学生
{
public string StudentName;
private string studentName
{获取 {返回 studentName; } set {studentName = value ; }

public string StudentLocation;
私有 字符串 studentLocation
{ get { return studentLocation; } set {studentLocation = value ;

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

// 构造函数
public 学生(){}

}

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

public string StudentsResult;
private string studentsResult
{获取 {返回 studentsResult; } set {studentsResult = value ; }

// 构造函数
public StudentSubjects(){}

}
}
}

解决方案




Forgive a stupid question but I am new to C# & OOP.

Can anyone help me prevent a "System.NullReferenceException:"?

I’m trying to do an assignment following instructions and using what we’ve learned to date (arrays, classes and constructors).

I’ve created an array of StudentSubjects classes and embedded this in an array of Student classes.

I want to print out details of each student’s subjects.

I can access the StudentArray[0] fields OK but can’t get to the StudentArray[0].StudentSubjectsArray[0] fields because "Object reference not set to an instance of an object"

I’ve spent 2 weeks looking for an answer but cannot find any examples of how to set

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

Any advice most appreciated.

What I have tried:

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() { }

        }
    }
}
解决方案




这篇关于C#(对象数组)对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 20:43