12

I'm trying to deserialize a json response and am getting the value cannot be null error.

Any help is really appreciated! I'm deserializing a lot of other json strings this way and have never run into this error. I'm not sure what's causing it. Thanks!

Here is the code for the object:

[Serializable]
public class LocationResponse
{
    public string authenticationResultCode { get; set; }
    public string brandLogoUri { get; set; }
    public string copyright { get; set; }
    public List<ResourceSet> resourceSets { get; set; }
    public int statusCode { get; set; }
    public string statusDescription { get; set; }
    public string traceId { get; set; }
}

[Serializable]
public class ResourceSet
{
    public int estimatedTotal { get; set; }
    public List<Resource> resources { get; set; }
}

[Serializable]
public class Resource
{
    //public string __type { get; set; }
    //public List<double> bbox { get; set; }
    public string name { get; set; }
    public Point point { get; set; }
    //public Address address { get; set; }
    //public string confidence { get; set; }
    //public string entityType { get; set; }
}

[Serializable]
public class Point
{
    public string type { get; set; }
    public List<double> coordinates { get; set; }
}

[Serializable]
public class Address
{
    public string countryRegion { get; set; }
    public string formattedAddress { get; set; }
}

The code to deserialize:

System.Web.Script.Serialization.JavaScriptSerializer ser = new System.Web.Script.Serialization.JavaScriptSerializer();
string json = "{\"authenticationResultCode\":\"ValidCredentials\",\"brandLogoUri\":\"http:\\/\\/dev.virtualearth.net\\/Branding\\/logo_powered_by.png\",\"copyright\":\"Copyright © 2011 Microsoft and its suppliers. All rights reserved. This API cannot be accessed and the content and any results may not be used, reproduced or transmitted in any manner without express written permission from Microsoft Corporation.\",\"resourceSets\":[{\"estimatedTotal\":1,\"resources\":[{\"__type\":\"Location:http:\\/\\/schemas.microsoft.com\\/search\\/local\\/ws\\/rest\\/v1\",\"bbox\":[33.177484847720336,35.531577579036423,33.235425613705445,35.623878963932327],\"name\":\"Qiryat Shemona, Israel\",\"point\":{\"type\":\"Point\",\"coordinates\":[33.206455230712891,35.577728271484375]},\"address\":{\"adminDistrict\":\"Northern\",\"countryRegion\":\"Israel\",\"formattedAddress\":\"Qiryat Shemona, Israel\",\"locality\":\"Qiryat Shemona\"},\"confidence\":\"High\",\"entityType\":\"PopulatedPlace\"}]}],\"statusCode\":200,\"statusDescription\":\"OK\",\"traceId\":\"NVM001351\"}";
LocationResponse response = ser.Deserialize<LocationResponse>(json);

I am getting an error and I can't figure out what part of the code or json is giving this error: Exception Details: System.ArgumentNullException: Value cannot be null. Parameter name: type

Here is the stack trace if that is helpful:

[ArgumentNullException: Value cannot be null.
Parameter name: type]
System.Activator.CreateInstance(Type type, Boolean nonPublic) +7468694
System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary`2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +406
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +71
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object& convertedObject) +147
System.Web.Script.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer) +21
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +181
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +119
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +210
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +422
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +147
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeList(Int32 depth) +119
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +210
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeDictionary(Int32 depth) +422
System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth) +147
System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer serializer) +51
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer serializer, String input, Type type, Int32 depthLimit) +37
System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(String input) +70

3 Answers 3

17

The problem is with the __type field in the JSON.

Reading the answer to the following: Problem with deserializing JSON on datamember “__type” it seems that quote: the "__type" field has a special meaning for DataContractJsonSerializer, denoting the type to which the object should be deserialized.

Removing __type from the JSON resolved the issue.

One option, (if you have no control over the JSON), I have just tested this with the JSON.NET library and it worked as expected, deserializing with no errors.

LocationResponse response = JsonConvert.DeserializeObject<LocationResponse>(json);
1
  • Excellent! spent much time surfing over web to find this solution
    – f0rza
    Jan 22, 2012 at 10:11
3

This is late, but I had the same issue and resolved it by adding a default constructor to the class in question and making sure the setters for the properties of that class were public. This resolved my issue (present with both FastJson and JSON.net).

Just in case anyone has an issue and the answers above don't help them.

1
  • I was just wondering this same thing and read yours and tried it right away. This fixed mine too! May 4, 2020 at 23:13
1
  1. The exception is thrown from System.Activator.CreateInstance(Type type, bool) method as you can see from stack trace.
  2. It is thrown because deserializer passes null as "type" to the method I mentioned above.

Most likely, it happens because deserializer is not able to find proper type to deserialize JSON to. Try to serialize instance of your LocationResponse class first and compare result with the string you're trying to deserialize.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.