WCF:对象引用未设置为对象的实例[重复]

时间:2022-03-05 14:30:49

This question already has an answer here:

这个问题在这里已有答案:

I have a CibilResponse Class that has properties that are of class type (TUEF class). I am trying to assign value to CibilEnquiryEnq.Tuef.Version but i am getting null reference error. Before you mark this question as duplicate let me mention that I have read some of the similar questions and their answers on SO and post which I have initialized tuef in the constructor as you can see in my code. Can you please point out if you can what is it that I am doing wrong?

我有一个CibilResponse类,它具有类类型的属性(TUEF类)。我正在尝试为CibilEnquiryEnq.Tuef.Version赋值,但我得到null引用错误。在你将这个问题标记为重复之前,让我提一下我已经阅读了一些类似的问题及其在SO上的答案以及我在构造函数中初始化tuef的帖子,正如你在我的代码中看到的那样。能不能指出你能做什么我做错了什么?

ICIBIL.cs

[ServiceContract]
public interface ICIBIL
{

    [OperationContract]
    string InsertCibil(CibilResponse cibilResponse);


    [OperationContract]
    string GenerateEnquiry(CibilEnquiry testObj);


}

[DataContract]
public class CibilResponse
{
    [DataMember]
    public string ResponseString { get; set; }
    [DataMember]
    public string Business { get; set; }
    [DataMember]
    public string MkrId { get; set; }
}


[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; }        

    }

Appication :

CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL();
CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry();
CibilEnquiryEnq.Tuef.Version = "123";// null reference error here
string res = obj.GenerateEnquiry(CibilEnquiryEnq);

2 个解决方案

#1


3  

Can you try the below. C# is case sensitive.

你能尝试以下吗? C#区分大小写。

using CbilFileReader.CibilWcfService;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


            CIBIL obj = new CIBIL();
            CibilEnquiry CibilEnquiryEnq = new CibilEnquiry();
            TUEF objtuef = new TUEF();
            objtuef.Version = "123";
            CibilEnquiryEnq.Tuef = objtuef;
            string res = obj.GenerateEnquiry(CibilEnquiryEnq);
        }
    }
}

#2


2  

This might do the trick for you

这可能对你有所帮助

CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL();
CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry();
CibilWcfService.CibilEnquiry.Tuef ObjTUEF = new CibilWcfService.CibilEnquiry.Tuef();
ObjTUEF.Version="123";
CibilEnquiryEnq.Tuef = ObjTUEF;
string res = obj.GenerateEnquiry(CibilEnquiryEnq);

#1


3  

Can you try the below. C# is case sensitive.

你能尝试以下吗? C#区分大小写。

using CbilFileReader.CibilWcfService;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {


            CIBIL obj = new CIBIL();
            CibilEnquiry CibilEnquiryEnq = new CibilEnquiry();
            TUEF objtuef = new TUEF();
            objtuef.Version = "123";
            CibilEnquiryEnq.Tuef = objtuef;
            string res = obj.GenerateEnquiry(CibilEnquiryEnq);
        }
    }
}

#2


2  

This might do the trick for you

这可能对你有所帮助

CibilWcfService.CIBIL obj = new CibilWcfService.CIBIL();
CibilWcfService.CibilEnquiry CibilEnquiryEnq = new CibilWcfService.CibilEnquiry();
CibilWcfService.CibilEnquiry.Tuef ObjTUEF = new CibilWcfService.CibilEnquiry.Tuef();
ObjTUEF.Version="123";
CibilEnquiryEnq.Tuef = ObjTUEF;
string res = obj.GenerateEnquiry(CibilEnquiryEnq);