0

I just copied the code from their official documentation and just changed "Console.WriteLine" to "lblDebug.Text".

string json = @"{
  'Table1': [
    {
      'id': 0,
      'item': 'item 0'
    },
    {
      'id': 1,
      'item': 'item 1'
    }
  ]
}";

DataSet dataSet = JsonConvert.DeserializeObject<DataSet>(json);

DataTable dataTable = dataSet.Tables["Table1"];

lblDebug.Text += dataTable.Rows.Count +"";
// 2

foreach (DataRow row in dataTable.Rows)
{
    lblDebug.Text += row["id"] + " - " + row["item"];
}

Soruce of the original sample: http://www.newtonsoft.com/json/help/html/DeserializeDataSet.htm

Everytime I compile it throws me a "System.NullReferenceException" in this line

lblDebug.Text += dataTable.Rows.Count +"";

and it seems like dataTable is 'null'.

Edit: I know what a NullReferenceException is, but why is it there? I mean it's a sample from the Json.Net page and how do I write the code correctly so I don't get the exception.

0 Answers0