访问数组时出现空引用错误
我有以下类:
public class MyClass
{
public MyArrObj[] arrOfObj; //Array of objects
}
当我尝试使用以下方式访问它时:
MyClass myClass = new MyClass;
myClass.arrOfObj = GetData();
它给了我一个空异常。如果它不是一个数组,我可以新建它,但我不知道如何处理这个问题。
I have the following class:
public class MyClass
{
public MyArrObj[] arrOfObj; //Array of objects
}
When I try to access it using:
MyClass myClass = new MyClass;
myClass.arrOfObj = GetData();
it gives me a null exception. If it wasn't an array, I could have new'd it, but i am not sure how to handle this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
创建引用类型的数组(在您的情况下是
MyArrObj
)时,除了分配内存之外,还需要为每个数组元素分配内存(使用new
运算符)对于数组本身。引用类型数组
例如,如果您创建 MyArrObj 数组,
则数组元素
objectArray[0]
到objectArray[2]
仍将为null
。需要单独初始化。只有完成上述步骤后,才能访问数组元素的成员。
如果跳过数组元素初始化,则尝试访问数组元素的成员将给出 <代码>System.NullReferenceException
因为
对象数组[0];
为null
。来检查这一点
您可以使用值类型数组
如果您事先知道数组,那么您可以使用@cloked指出的初始化。
对于内置类型等值类型(例如
int、float
、结构体等),您无需初始化每个数组元素。例如,如果您创建一个 int 数组,
那么数组元素
intArray[0]
到intArray[2]
就分配了内存,并且可以将值分配给他们。检查您的 GetData()
因此,您的
GetData()
方法应包含如下代码:更多信息
When creating an array of a reference type (
MyArrObj
in your case), it is required that you allocate memory for each array element (usingnew
operator) in addition to allocating memory for the array itself.Array of Reference types
For example, if you create an MyArrObj array,
Then the array elements
objectArray[0]
toobjectArray[2]
would still benull
. A separate initialization is required.Only when the above step is done can you access the members of the array element.
If you skip the array element initialization then trying to access a member of the array element would give a
System.NullReferenceException
because
objectArray[0];
isnull
.You can check this using
Array of Value types
If you know the array before hand, then you can use the initialization pointed out by @clonked.
For value types like built in types(like
int, float
, structs, etc. you need not initialize each array element.For example, if you create an int array,
Then the array elements
intArray[0]
tointArray[2]
have the memoery allocated and values can be assigned to them.Check your GetData()
So your
GetData()
method should include code like this,More information
您使用
GetData()
方法的方式,它必须返回一个数组。这里有几种动态创建数组的方法(不过,您仍然需要填充它们):
干杯。
The way your are using your
GetData()
method, it has to return an array.Here's a couple of ways to create an array on the fly (you'll still need to populate them, though):
Cheers.
您需要做这样的事情:
GetData 方法是重要的部分。
它必须初始化一个新数组并返回它。
You need to do something like this:
the GetData method is the important part.
it has to initialize a new array and return it.
如果您的
GetData()
方法的签名类似于public MyArrObj[] GetData()
那么您的代码应该可以正常工作。我使用 2 个版本的“GetData”方法测试了您的代码:这些方法都没有引发空引用异常。我怀疑您的 GetData() 方法内部还有其他东西抛出空引用,而不是将该方法的结果分配给 arrOfObj 字段的操作。
If the signature of your
GetData()
method looks likepublic MyArrObj[] GetData()
then your code should work correctly. I tested your code with 2 versions of the 'GetData' method:Neither of these methods threw a null reference exception. I would suspect there is something else inside of your
GetData()
method that is throwing the null reference, rather than the operation of assigning the results of that method to thearrOfObj
field.