1

我的Mainclass代码中有一个包含各种列表和各种子集的代码。当我尝试

main k = new main();
k.main.addressinfo.addressline1 = "XXX";

我收到错误“对象引用未设置为对象的实例//NULLEXCEPTION。”

public class Mainclass
{
    public List<main> mainset { get; set; }
 // do sth to load and save model info
}
 public class main
{
    public personalinfo info { get; set; }
    public addressinfo currentaddr { get; set; }
    public addressinfo[] otheraddr { get; set; }
    public telephone currenttel { get; set; }
    public telephone[] othertel { get; set; }
 }
public class addressinfo
{
    public string Addressline1 { get; set; } 
    public string Addressline2 { get; set; }
    public string City { get; set; }
    public string postcode { get; set; 
}
public class telephone
{
    public int tel { get; set; }
}

由于该类包含列表和数组,我对如何将字符串的默认值设置为 EMPTY 但不是 NULL 有点困惑。另外,我如何确保默认情况下 Childrens 有一个 EMPTY 但不是 NULL 对象?

4

3 回答 3

3

你需要初始化你的对象

public personalinfo info = new personalinfo();
public addressinfo currentaddr = new addressinfo();
public telephone currenttel = new telephone();
于 2013-06-29T21:06:31.830 回答
0

您必须为您的类定义构造函数,并在其中将属性设置为默认值。

于 2013-06-29T21:15:14.277 回答
0

@Using mcmonkey4eva回答

public personalinfo info = new personalinfo();
public addressinfo currentaddr = new addressinfo();
public telephone currenttel = new telephone();

public addressinfo[] otheraddr = new addressinfo[1];
public List<main> mainset = new List<main>();
于 2013-06-29T21:41:32.313 回答