0

Consider the following tutorial: http://blogs.msdn.com/b/rakkimk/archive/2009/01/30/asp-net-json-serialization-and-deserialization.aspx

{
    "firstName" : "Rakki",
    "lastName" : "Muthukumar",
    "department" : "Microsoft PSS",
    "address" : {
        "addressline1" : "Microsoft India GTSC",
        "addressline2" : "PSS - DSI",
        "city" : "Bangalore",
        "state" : "Karnataka",
        "country" : "India",
        "pin" : 560028
    },
"technologies" : ["IIS", "ASP.NET", "JavaScript", "AJAX"]    
}

For the json code I have the following class:

    public class Address
{
public string addressline1 { get; set; }
public string addressline2 { get; set; }
public string city { get; set; }
public string state { get; set; }
public string country { get; set; }
public int pin { get; set; }
}

public class RootObject
{
public string firstName { get; set; }
public string lastName { get; set; }
public string department { get; set; }
public Address address { get; set; }
public List<string> technologies { get; set; }
}

The above properties give me error when I'm trying to set data like

RootObjectClsObject.address.addressline1 = "NO";

It throws me NullReferrenceException. If I modify the line

public List<string> technologies { get;set;}

by following line

public List<string> technologies = new List<string>();

Which I don't want to use. Because I've complex types of JSON which can't processed in this way.

2
  • How do you declare the RootObjectClsObject variable? Commented Jan 14, 2013 at 11:38
  • Normal way RootObject RootObjectClsObject = new RootObject(); Commented Jan 14, 2013 at 11:39

2 Answers 2

1

You are not initialising your sub-objects. The address property has not been assigned an instance of the Address object yet, so when you are attempting to update the address1 property; the application does not know which object you are referring to - hence the error.

You simply need to assign a value to the address property or initialise it with a default value.

The simplest way to do this would be in the default constructor for you RootObject class.

public class RootObject
{
   public RootObject()
   {
      address = new Address();
      technologies = new List<string>();
   }

    public string firstName { get; set; }
    public string lastName { get; set; }
    public string department { get; set; }
    public Address address { get; set; }
    public List<string> technologies { get; set; }
}
Sign up to request clarification or add additional context in comments.

Comments

1

As @Kami suggested, you could initialize the address and technologies in the RootObject's constructor, or you could assign the addressline1 (and the other address object's properties) using the following syntax:

RootObjectClsObject.address = new Address()
{
    addressline1 = "NO"
};

1 Comment

Thanks for your info. But since @Kami's solution is more useful for my case. I'll go with that one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.