简体   繁体   English

C#未处理的异常:对象引用未设置为对象的实例

[英]c# unhandled exception: object reference not set to an instance of an object

I have a problem with an array of classes. 我对一组类有问题。 basically I create an array of the class 'Student', but I cannot assign values to any properties of the instances in the array because I get the unhandled exception in the title. 基本上,我创建了一个'Student'类的数组,但是由于在标题中得到了未处理的异常,因此无法将值分配给该数组中实例的任何属性。 here's the code: 这是代码:

class ControllerQueue
{
    model.ModelStudent q = new model.ModelStudent();

    static int capacity = 15;
    model.ModelStudent[] arr = new model.ModelStudent[capacity];
    int top = -1, rear = 0;

    public ControllerQueue()
    {
        arr[0].name = "a";
        arr[0].grade = 0;
    }
}

I tried assigning values out of the constructor, but I still get the same result. 我尝试从构造函数中分配值,但仍然得到相同的结果。 now, the exception itself obviously sais I didn't instantiate the Student class but I don't understand why it would say that, I've already instantiated it. 现在,异常本身显然表示我没有实例化Student类,但是我不明白为什么会这样说,我已经实例化了。 thanks in advance. 提前致谢。

You need 你需要

arr[0] = new ModelStudent();
arr[0].name = "a";
arr[0].grade = 0;

You need this because you have to new up an instance to put into the array at index 0 您需要这样做,因为您必须new一个实例以将其放入索引为0的数组中

model.ModelStudent[] arr = new model.ModelStudent[capacity];

Will just allocate the array, but each entry is by default the default value of ModelStudent (null) 只会分配数组,但是默认情况下每个条目都是ModelStudent的默认值(null)

you need to instantiate the members of your array 您需要实例化数组的成员

add the item to your array like this 像这样将项目添加到您的数组中

arr[0] = new ModelStudent();
arr[0].name = "a";
arr[0].grade = 0;

Your item 0 is not set. 您的项目0未设置。

Try. 尝试。

model.ModelStudent q = new model.ModelStudent();

        static int capacity = 15;
        model.ModelStudent[] arr = new model.ModelStudent[capacity];
        int top = -1, rear = 0;


        public ControllerQueue()
        {
            arr[0] = new model.ModelStudent();
            arr[0].name = "a";
            arr[0].grade = 0;
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C#未处理的异常:System.NullReferenceException:对象引用未设置为对象的实例 - C# Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object 未处理C#NullReferenceException-未将对象引用设置为对象的实例 - C# NullReferenceException was unhandled - Object reference not set to an instance of an object C#GetMethod抛出异常:对象引用未设置为对象的实例 - C# GetMethod throw exception: Object reference not set to an instance of an object c# linq 异常:未将对象引用设置为对象的实例 - c# linq exception: object reference not set to an instance of an object C#对象引用未设置为对象的实例 - C# Object reference not set to an instance of an object C# - “对象引用未设置为对象的实例” - C# - “Object reference not set to an instance of an object” c# Object 参考未设置为 object 的实例 - c# Object reference not set to an instance of an object C#对象引用未设置为对象的实例 - C# Object reference not set to an instance of an object 对象引用未设置为C#中的对象实例 - Object reference not set to an instance of an object in C# 对象引用未设置为对象的实例-C# - Object reference not set to an instance of an object - C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM