null value exception thrown when deserializing null value JSON.net null value exception thrown when deserializing null value JSON.net json json

null value exception thrown when deserializing null value JSON.net


I had the same exact error message as I tried calling the same method. Make sure you have a default constructor (constructor with no parameters) for your target class (your testContract class).

In C# your class and default constructor would look something like this:

class testContract{    string StringProperty;    int IntegerProperty;    public testContract()    {        // This is your default constructor. Make sure this exists.        // Do nothing here, or set default values for your properties        IntegerProperty = -1;    }    public testContract(string StringProperty, int IntegerProperty)    {        // You can have another constructor that accepts parameters too.        this.StringProperty = StringProperty;        this.IntegerProperty = IntegerProperty;    }}

When JSON.net wants to deserialize a JSON string into an object, it first initializes the object using its default constructor and then starts populating its properties. If it doesn't find a default constructor, it will initialize the object using whatever other constructor that it can find, but it will pass null to all parameters.

In a nutshell, You should either have a default constructor for your target class, or, your non-default constructor must be able to handle all null parameters.


if you use [Serializable] you already should have your default ctor otherwise it can't be part of data binding. checkout

  [JsonPropertyAttribute("jsonProp", Required=Required.Default)] 

on the property works for me

Newtonsoft has to methods

Parse - will parse partial dataandDeserialize - will parse entire data

if you wish to use partial data, like in the example on their website, use Parse.

If you wish to use Deserialize you need to make sure all your properties exist and are marked with Default like i wrote above.