是否可以在c#中的复杂元素中发送复杂元素的列表?

是否可以在c#中的复杂元素中发送复杂元素的列表?,c#,C#,在我的web服务中,我必须在复杂元素中传递复杂元素列表 我的第一节课是: 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 mod

在我的web服务中,我必须在复杂元素中传递复杂元素列表

我的第一节课是:

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; }
现在我想把上面的类发送给我的方法。 但当我将第二个类添加到第一个类的列表中时,我得到了以下错误

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;
test.request s=new test.request();
test.JBCredential jb=new test.JBCredential();
jb.code=“LNK”;
jb.Username=“”;
jb.Password=“”;
/
s、 query=“java”;
s、 mode=“模拟搜索”;
s、 JBCredentials.Add(jb)//我没有得到add方法
所以我试过这样做
s、 JBCredentials[0]。代码=jb.code;
s、 JBCredentials[0]。密码=jb.Password;
s、 JBCredentials[0]。用户名=jb.Username;
我也试过了
列表ad=新列表();
ad.Add(jb);
s、 jb=ad;

我可以从普通请求向JBcredentials类型传递值,但在调用web服务时无法传递此类型。

请确保在构造函数中初始化列表:

public ComplexClass {
  public ComplexClass() {
    this.JBCredentials = new List<JBCredential>();
  }
}
公共复杂类{
公共ComplexClass(){
this.JBCredentials=新列表();
}
}

请说明您是如何添加和其他代码的。我已经在我的构造函数中初始化了该代码,您确定吗?列表的NRE通常意味着它们没有正确初始化。
public ComplexClass {
  public ComplexClass() {
    this.JBCredentials = new List<JBCredential>();
  }
}