7

I have a simple JSON generated with WCF Rest client, but when I try to deserialize a response I get an error NullReferenceException in JSON.Net. I have following JSON:

{"Code":-2146232800,"ExceptionType":"IOException","Message":"Msg","Stacktrace":"Some"}

And following class:

[DataContract]
public class FileUploadError
{
    public FileUploadError(Exception exception)
    {
        Code = exception.HResult;
        ExceptionType = exception.GetType().Name;
        Message = GetMessage(exception);
        Stacktrace = exception.StackTrace;
        if (exception.Data.Count > 0)
        {
            Data = string.Join(Environment.NewLine, exception.Data.Cast<DictionaryEntry>().Select(x => x.Key + "=" + x.Value));
        }
    }

    private string GetMessage(Exception exception)
    {
        if (exception.InnerException == null)
        {
            return exception.Message;
        }
        const string delimiter = "->";
        var sb = new StringBuilder(1024);
        for (var ex = exception; ex != null; ex = ex.InnerException)
        {
            sb.Append(ex.Message).Append(delimiter);
        }
        sb.Length -= delimiter.Length;
        return sb.ToString();
    }

    [DataMember(IsRequired = true)]
    [JsonProperty("Code", NullValueHandling = NullValueHandling.Ignore)]
    public int Code { get; set; }
    [DataMember(IsRequired = true)]
    [JsonProperty("ExceptionType", NullValueHandling = NullValueHandling.Ignore)]
    public string ExceptionType { get; set; }
    [DataMember(EmitDefaultValue = false)]
    [JsonProperty("Message", NullValueHandling = NullValueHandling.Ignore)]
    public string Message { get; set; }
    [DataMember(EmitDefaultValue = false)]
    [JsonProperty("Stacktrace", NullValueHandling = NullValueHandling.Ignore)]
    public string Stacktrace { get; set; }
    [DataMember(EmitDefaultValue = false)]
    [JsonProperty("Data", NullValueHandling = NullValueHandling.Ignore)]
    public string Data { get; set; }
}

Then I deserialize it:

const string text = "{\"Code\":-2146232800,\"ExceptionType\":\"IOException\",\"Message\":\"Msg\",\"Stacktrace\":\"Some\"}";
var obj = JsonConvert.DeserializeObject<FileUploadError>(text);

And get subj error. Reproduced in new console application, which I can provide if required. JSON is very simple, why I am getting this error?

Sample: https://dotnetfiddle.net/76FJd0

Stacktrace:

System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.NullReferenceException: Object reference not set to an instance of an object.
   at FileUploadError..ctor(Exception exception) in d:\Windows\Temp\nclgvim3.0.cs:line 28
   --- End of inner exception stack trace ---
   at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
   at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.ConstructorInfo.Invoke(Object[] parameters)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObjectFromNonDefaultConstructor(JsonReader reader, JsonObjectContract contract, JsonProperty containerProperty, ConstructorInfo constructorInfo, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at Program.Main(String[] args)
Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • @Nasreddine do you read before marking? I provided a string that is constant, do you think constant non-empty strign could be null? – Alex Zhukovskiy May 14 '16 at 18:05
  • 2
    This **is** a duplicate of the linked question and answer. If you had debugged the code you would have realized that `exception.HResult` would throw that exception since your `exception` parameter is `null`. Since you're *not* asking "why is my exception parameter null" then the question is a duplicate. – Lasse V. Karlsen May 14 '16 at 18:12
  • Voted to re-open to remove 'dupe' flag. This may be a duplicate of *some* question about Json.Net deserialization, but it is certainly not a duplicate of the linked question in any useful way. – McKenzieG1 Apr 07 '17 at 18:08
  • @McKenzieG1 What got solved is OP specific case, not the `NullReferenceException` which doesn't give any info. The answer should provide a way to solve that first and foremost. – jeromej Sep 06 '18 at 08:45

2 Answers2

13

You need to add a public parameterless constructor to your FileUploadError class:

public class FileUploadError
{
    public FileUploadError()
    {
    }

Or, you could make it private and use [JsonConstructor]:

public class FileUploadError
{
    [JsonConstructor]
    private FileUploadError()
    {
    }

Or, leave it private and deserialize with ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor:

var settings = new JsonSerializerSettings
{
    ConstructorHandling = ConstructorHandling.AllowNonPublicDefaultConstructor
};
var obj = JsonConvert.DeserializeObject<FileUploadError>(text, settings);

with:

public class FileUploadError
{
    private FileUploadError()
    {
    }
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Wonderful attribute, thank you. Didn't know about it. – Alex Zhukovskiy May 14 '16 at 18:17
  • This one seems to keep biting me in the backside over and over again. Thanks! – Francis Mar 06 '18 at 05:26
  • What's the solution to get a clearer error message? I tried this but it didn't fix it for me, obviously my case is different but we can't expect everyone to constantly share theirs because json.net doesn't give clear errors. – jeromej Sep 06 '18 at 08:43
3

For those tumbling upon this later and since OP's specific case has been solved.

Here is what to do to get a clearer error message when it happens.

Source: https://www.newtonsoft.com/json/help/html/SerializationErrorHandling.htm

var errors = new List<string>();

List<DateTime> c = JsonConvert.DeserializeObject<List<DateTime>>(
    yourJsonHere,
    new JsonSerializerSettings
    {
        Error = delegate(object sender, ErrorEventArgs args)
        {
            errors.Add(args.ErrorContext.Error.Message);
            args.ErrorContext.Handled = true;
        }
    });

And errors will contain a list of the errors that occured! Works both for serialization and deserialization!

Doing so you do lose the context though! So alternatively you can set it to Handled = false.

jeromej
  • 10,508
  • 2
  • 43
  • 62