0
    public class BClass
    {
    public class RClass
        {
            public string stjCd { get; set; }
            public string lgnm { get; set; }
            public string stj { get; set; }
            public string dty { get; set; }
            public List<object> adadr { get; set; }
            public string cxdt { get; set; }
            public string gstin { get; set; }
            public List<string> nba { get; set; }
            public string lstupdt { get; set; }
            public string rgdt { get; set; }
            public string ctb { get; set; }
            public Pradr pradr { get; set; }
            public string tradeNam { get; set; }
            public string sts { get; set; }
            public string ctjCd { get; set; }
            public string ctj { get; set; }
        }


      public class AClass
        {
            public string id { get; set; }
            public string consent { get; set; }
            public string consent_text { get; set; }
            public int env { get; set; }
            public string response_code { get; set; }
            public string response_msg { get; set; }
            public int transaction_status { get; set; }
            public string request_timestamp { get; set; }
            public string response_timestamp { get; set; }
            public RClass result { get; set; }
        }
}






//COntroller

 BClass.AClass btr = new BClass.AClass();  
var lst = JsonConvert.DeserializeObject<BClass.AClass>(strresult);
          btr.response_code = lst.response_code;
            btr.response_msg = lst.response_msg;
            btr.result.lgnm = lst.result.lgnm;  

The property btr.result.lgnm = lst.result.lgnm; Gives null value error object reference not set to instance of an object. but the lst variable has a value in the response received.Please provide suggesion

  • Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Peter Duniho Apr 17 '20 at 03:54

2 Answers2

1

You can solve this by adding one line into your code.

btr.result = new BClass.RClass(); //This one. You need to initialize instance before assigning anything to it. 
btr.result.lgnm = lst.result.lgnm;

or else, you can also create default constructor for class A.

public AClass()
{
    result = new RClass();
}

I would suggest you to please have a look at below web resources for naming conventions widely used for c# language.

Properties naming conventions: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-type-members

class naming conventions: https://learn.microsoft.com/en-us/dotnet/standard/design-guidelines/names-of-classes-structs-and-interfaces

Jayesh Tanna
  • 398
  • 5
  • 17
0

Assigning this way does not give null reference exception

RClass rclass=new RClass();
rclass.lgnm=lst.result.lgnm