title: Newtonsoft Json Error converting value {null} to type 'System.Int32'
body: <p>When performing an AJAX request I am getting the following error:</p>
  
  <blockquote>
    <p>Error converting value {null} to type 'System.Int32'. Path '[5].tabID', line 1, position 331.</p>
  </blockquote>
  
  <p>The error occurs on the second line of my <code>processRequest (...)</code></p>
  
  <pre><code>public void ProcessRequest (HttpContext context) { 
      string strJson = new StreamReader(context.Request.InputStream).ReadToEnd();
      List&lt;ElementToUpdate&gt; elements = JsonConvert.DeserializeObject&lt;List&lt;ElementToUpdate&gt;&gt;(strJson);
  
      // (...)
  }
  </code></pre>
  
  <p>The debugger says that this the content of <code>strJson</code>:</p>
  
  <pre><code>[{
      "bmk": "132M1",
      "state": "off",
      "type": "motor",
      "tabID": 8
  }, {
      "bmk": "158M1",
      "state": "off",
      "type": "motor",
      "tabID": 8
  }, {
      "bmk": "194M1",
      "state": "off",
      "type": "motor",
      "tabID": 8
  }, {
      "bmk": "198M1",
      "state": "on",
      "type": "motor",
      "tabID": 8
  }, {
      "bmk": "202M1",
      "state": "off",
      "type": "motor",
      "tabID": 8
  }, {
      "bmk": "test-m",
      "state": "on",
      "type": "motor",
      "tabID": null
  }, {
      "bmk": "158M1-2",
      "state": "off",
      "type": "motor",
      "tabID": 2
  }, {
      "bmk": "100M1",
      "state": "on_right",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "152M1",
      "state": "on",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "192M1",
      "state": "on_left",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "196M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "2000M1",
      "state": "on_left",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "74M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "76M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "80M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "82M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "86M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "90M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "94M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "95M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "96M1",
      "state": "off",
      "type": "screwconveyor",
      "tabID": 8
  }, {
      "bmk": "102Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "104Y1",
      "state": "open",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "112Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "114Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "120Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "122Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "128Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "146Y1_2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "148Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "156Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "180Y1",
      "state": "open",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "182Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "184Y1",
      "state": "open",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "206Y1",
      "state": "open",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "208Y1",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "72Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "78Y2",
      "state": "open",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "84Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "88Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "92Y2",
      "state": "closed",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "95_1Y1",
      "state": "blocked",
      "type": "ventile",
      "tabID": 8
  }, {
      "bmk": "17H1",
      "state": "on",
      "type": "lamp",
      "tabID": 8
  }, {
      "bmk": "l1",
      "state": "on",
      "type": "lamp",
      "tabID": 8
  }, {
      "bmk": "17H1-2",
      "state": "on",
      "type": "lamp",
      "tabID": 2
  }, {
      "bmk": "106M1",
      "state": "on",
      "type": "elevator",
      "tabID": 8
  }, {
      "bmk": "154M1",
      "state": "off",
      "type": "elevator",
      "tabID": 8
  }, {
      "bmk": "164M1",
      "state": "off",
      "type": "rotaryvalve",
      "tabID": 8
  }]
  </code></pre>
  
  <p>The class <code>ElementToUpdate</code> is</p>
  
  <pre><code>public class ElementToUpdate
  {
  public ElementType type;
  public String bmk;
  public string state;
  public int tabID;
  
  public ElementToUpdate()
  {
  }
  
  public ElementToUpdate(ElementType type, String bmk, string state, int tabID)
  {
      this.type = type;
      this.bmk = bmk;
      this.state = state;
      this.tabID = tabID;
  }
  }
  </code></pre>
  
  <p>So my question is: How to resolve this issue? If I understand the error message correctly, then it says that <code>tabID</code> of the 5th json object in the serialized array is <code>null</code>. But as you can see it isn't. Moreover <code>ElementToUpdate.tabID</code> isn't an <code>Int32</code> but an <code>int</code>. Did I miss something?</p>
  
  <p><strong>The solution</strong></p>
  
  <p>In fact, my JSON string contained an element whose <code>tabID</code> was <code>null</code>. I somehow overlooked this because firstly my JSON string wasn't formatted when I checked and secondly because [5] means "6th element of the array" (which I claimed to know actually).</p>