首页 新闻 会员 周边

C#中为另一个类中的静态数组赋值异常

0
[待解决问题]

//////A方法
public class test
{
public Class s
{
public string str;
public int number;
}

public static s[] t=new s[4]
}
//////////////


///////方法B
using ******;

public Class Do
{
test.t[0].str="Hello";
test.t[1].str="World";
test.t[2].str="Oh";
test.t[3].str="God";

test.t[0].number=1;
test.t[1].number=2;
test.t[2].number=3;
test.t[3].number=4;
}

会出现System.NullReferenceException:“未将对象引用设置到对象的实例。”的异常.

CodeNoError的主页 CodeNoError | 菜鸟二级 | 园豆:204
提问于:2018-08-07 16:20
< >
分享
所有回答(1)
0

问题原因:

数组内部元素没有实例化!

解决方法:

实例化数组中每个元素。

public static s[] t=new s[4]

t[0]=new s();
test.t[0].str="Hello";

CodeNoError | 园豆:204 (菜鸟二级) | 2018-08-07 17:09
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册