在我的网络服务中,我必须传递复杂元素中的复杂元素列表。
我的第一堂课是:
public List<JBCredential> JBCredentials{ get; set; }
public string codes { get; set; }
public string db { get; set; }
public string locale { get; set; }
public string location { get; set; }
public string mode { get; set; }
public string postcode { get; set; }
在上面的课程中,我将发送另一个课程的列表,如下所示:
public string code { get; set; }
public string Username { get; set; }
public string Password { get; set; }
现在我想将上面的类发送给我的方法。 但是当我将第二个类添加到第一类列表时,我收到以下错误
Object reference not set to an instance of an object.
请帮我解决这个问题。
我尝试了以下
test.request s=new test.request();
test.JBCredential jb = new test.JBCredential();
jb.code = "LNK";
jb.Username = "";
jb.Password = "";
/
s.query = "java";
s.mode = "mock_search";
s.JBCredentials.Add(jb);//I'm not getting add method
so I've tried like this
s.JBCredentials[0].code = jb.code;
s.JBCredentials[0].Password = jb.Password;
s.JBCredentials[0].Username = jb.Username;
and also I tried
List<JBCredential> ad=new List<JBCredential>();
ad.Add(jb);
s.JBCredentials=ad;
我能够从普通请求将值传递给JBcredentials类型,但是当我调用Web服务时,我无法传递此类型。
答案 0 :(得分:1)
确保在构造函数中初始化列表:
public ComplexClass {
public ComplexClass() {
this.JBCredentials = new List<JBCredential>();
}
}