I have a CibilResponse Class that has properties that are of class type (TUEF class).
I am trying to assign values using : CibilEnquiryEnq.Tuef.Version = "12";
but it throws null reference error. I have already solved this error but through creating an object like : CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF();
and not through constructor.
ICIBIL.cs
[ServiceContract]
public interface ICIBIL
{
[OperationContract]
string InsertCibil(CibilResponse cibilResponse);
[OperationContract]
string TestInsert(string testObj);
[OperationContract]
string GenerateEnquiry(CibilEnquiry testObj);
}
[DataContract]
public class CibilEnquiry
{
[DataMember]
public TUEF Tuef { get; set; }
public CibilEnquiry()
{
this.Tuef = new TUEF();
}
}
[DataContract]
public class TUEF
{
[DataMember]
public string SegmentTag { get; set; }
[DataMember]
public string Version { get; set; }
[DataMember]
public string MemberReferenceNumber { get; set; }
}
Application:(not working)
CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL();
CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry();
CibilEnquiryEnq.Tuef.Version = "1111"; // null reference error here and Tuef is null
Application:(working)
CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL();
CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry();
CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF();
CibilEnquiryEnq.Tuef.Version = "1111";//works fine
I don't understand why I have to add CibilEnquiryEnq.Tuef = new CibilWcfService.TUEF();
to make this work. I am already initializing tuef in constructor in my wcf.
I created a sample in a console application (excluded wcf) and it worked fine without having Tuef = new TUEF();
, initializing in constructor was enough.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…