CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2008
    Posts
    111

    Question JSON.net class creating array error Object reference not set to an instance of an ob

    I am trying to do the following:
    Code:
        {
        	"name": "Bob Barker",
        	"devName": "InformationServices",
        	"ReturnedData": [{
        		"level_heading": "blah1",
        		"DeliverBestMedicalValue": "blah2",
        		"level_question": "blah3"
        	}]
        }
    I can add the **name** and **devName** just fine but I am unsure on how to go about adding the **ReturnedData** part of the array so that it will return as the layout above?

    Example code I am using (CLASS):
    Code:
        public class febRecords
        {        
            [JsonObject]
            public class theOutput
            {
    
                [JsonProperty(PropertyName = "name")]
                public string Name { get; set; }
    
                [JsonProperty(PropertyName = "devName")]
                public string DevName { get; set; }
    
                [JsonProperty(PropertyName = "ReturnedData")]
                public List<ReturnedData> ReturnedData { get; set; }
            }
    
            [JsonObject]
            public class ReturnedData
            {
                [JsonProperty(PropertyName = "level_heading")]
                public string level_heading { get; set; }
    
                [JsonProperty(PropertyName = "level_question")]
                public string level_question { get; set; }
                
                [JsonProperty(PropertyName = "level_1_text")]
                public string level_1_text { get; set; }
    
                [JsonProperty(PropertyName = "level_1_status")]
                public string level_1_status { get; set; }
    
                [JsonProperty(PropertyName = "level_1_date")]
                public string level_1_date { get; set; }
    
                [JsonProperty(PropertyName = "level_1_dd")]
                public string level_1_dd { get; set; }
            }
    And the c#:
    Code:
       febRecords.RootObject febRecordsData = JsonConvert.DeserializeObject<febRecords.RootObject>(serverResponse);
       febRecords.theOutput febFormData = new febRecords.theOutput();
    
       febFormData.ReturnedData.Add(new febRecords.ReturnedData() { 
          level_heading = headings[x], 
          level_question = questions[x],
          level_1_text = _tmplevelTotal[0],
          level_1_status = _tmplevelTotal[1],
          level_1_date = _tmplevelTotal[2],
          level_1_dd = _tmplevelTotal[3]
       });
    
    totalCount += 1;
    When I try doing the above I get an error on the febFormData.ReturnedData.Add(...); part stating:

    Object reference not set to an instance of an object.
    Any help would be great!

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: JSON.net class creating array error Object reference not set to an instance of an

    Add a constructor to the theOutput class and initialize the list.

    Code:
    public theOutput ()
    {
      ReturnedData = List <ReturnedData>();
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured