0

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

我的第一堂课是:

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 服务时我无法传递这种类型。

4

1 回答 1

1

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

public ComplexClass {
  public ComplexClass() {
    this.JBCredentials = new List<JBCredential>();
  }
}
于 2013-11-02T07:16:41.673 回答