为什么我得到"对象引用未设置为对象&QUOT的实例;访问一个新的数组的元素是什么时候? [英] Why do I get "object reference not set to instance of object" when accessing elements of a new array?

查看:202
本文介绍了为什么我得到"对象引用未设置为对象&QUOT的实例;访问一个新的数组的元素是什么时候?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下类

 公共类alumno
{
    公共字符串NOMBRE,matricula;
    公众诠释semestre;

    公共字符串[] materias =新的字符串[5];
    公共双[]加利福尼亚=新的双[5];
}
 

和我需要建立一个数组,但我碰到一个错误访问它的时候。

 静态无效的主要(字串[] args)
{
    alumno [] ALS =新alumno [5];
    alumno人=新alumno(); //这里我没有问题
    al.nombre =天使; //这里我没有问题

    ALS [0] .nombre =天使; //但在这里我有问题
    ALS [0] .semestre = 6;
    ALS [0] .matricula =123;
    ALS [0] .materias [0] =西班牙语;
    ALS [0] .calif [0] = 10;
}
 

我收到的错误是Referencia一个objeto没有establecida科莫instancia德UN objeto,这在 英文的意思是未将对象引用设置到对象的实例

我如何填写我的对象在阵列?

解决方案

  alumno [] ALS =新alumno [5];
 

这将创建一个包含5个元素的数组,但每个元素都必须的的创建。

  ALS [0] =新alumno();
//现在你可以访问ALS [0] .nombre
 

您需要在数组中做到这一点的每一个元素,然后才能访问其成员。

 的(INT指数= 0;指数< als.Length;指数++)
{
     ALS [指数] =新alumno();
}
 

什么,你所遇到的正式名称为的NullReferenceException 。这是不合法的访问一个空引用成员(属性,方法)。当您创建一个数组,数组中的每个元素将被设置为类型的默认值。对于类,默认值为null,这就是为什么你遇到了你的问题。


无关你直接的问题,有一些事情你应该知道编写C#code时。这是惯例大写类名和类的公共成员。

 类Alumno
{
     公共字符串农布雷{获得;组; }
}
 

这也凸显了另外一个惯例:它不是地道的C#揭露成员字段(类级别的变量)向社会公布。这些都是存隐患。我们通过公开数据的属性的在C#code(也有例外,这些都是比较你所说的指南的不是实际的规则)​​。

I have the following class

public class alumno
{
    public string nombre, matricula;
    public int semestre;

    public string []materias = new string [5];
    public double[] calif = new double[5];
}

And I need to create an array, but I run into an error when accessing it.

static void Main(string[] args)
{
    alumno[] als = new alumno[5];
    alumno al = new alumno(); // here i dont have problem
    al.nombre = "angel"; // here i dont have problem

    als[0].nombre = "angel"; // but here i DO have problem    
    als[0].semestre = 6;
    als[0].matricula = "123"; 
    als[0].materias[0] = "español";
    als[0].calif[0] = 10;
}

The error I receive is "Referencia a objeto no establecida como instancia de un objeto", which in english means "Object reference not set to an instance of an object"

How can I fill my objects on array?

解决方案

alumno[] als = new alumno[5]; 

This creates an array that contains 5 elements, but each element must also be created.

als[0] = new alumno();
// now you can access als[0].nombre

You will need to do this for each element in the array before you can access its members.

for (int index = 0; index < als.Length; index++)
{
     als[index] = new alumno();
}

What you have run into is formally known as a NullReferenceException. It is not legal to access the members (properties, methods) of a null reference. When you create an array, each element in the array is set to the default value for the type. For classes, the default value is null, and this is why you have run into your issue.


Unrelated to your direct issue, a few things you should be aware of when writing C# code. It is convention to capitalize class names and public members of classes.

class Alumno
{
     public string Nombre { get; set; }
}

This also highlights another convention: it is not idiomatic in C# to expose member fields (class level variables) to the public. Those are kept hidden. We expose data via properties in C# code (there are exceptions, these are more what you call guidelines than actual rules).

这篇关于为什么我得到&QUOT;对象引用未设置为对象&QUOT的实例;访问一个新的数组的元素是什么时候?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆