1

I'm using the plugin Newtonsoft.Json in my xamarin.forms application for serializing and deserializing an object of typr Form

public class Form
{
    [JsonProperty("id")]
    public int id { set; get; }

    [JsonProperty("title")]
    public string title { set; get; }

    [JsonProperty("body")]
    public string body { set; get; }

    [JsonProperty("department_id")]
    public string department_id { set; get; }

    [JsonProperty("fields")]
    public List<FormField> fields { set; get; }
}

public class FormField
{
    [JsonProperty("id")]
    public int id { set; get; }

    [JsonProperty("title")]
    public string label { set; get; }

    [JsonProperty("inputtype")]
    public string inputtype { set; get; }

    [JsonProperty("key")]
    public string key { set; get; }

    [JsonProperty("item_order")]
    public int order { set; get; }

    [JsonProperty("required")]
    public bool isRequired { set; get; }

    [JsonProperty("enabled")]
    public bool isEnabled { set; get; }

    public CellCustom fieldObject { set; get;}


    public FormField()
    {
        fieldObject = CreateInstance() as CellCustom;
    }

    private object CreateInstance()
    {
        return Activator.CreateInstance(Type.GetType("Hura.Models.Cells." + inputtype));
    }

    public Cell createCell()
    {
        return fieldObject.createCell();
    }
}

here is the deserializing code

string str= @"{""id"": 17,""title"": ""testform"",""body"": ""null"",""department_id"": 5,""fields"": [{""id"": 28,""title"": ""null"",""inputtype"": ""text"",""key"": ""f1474532070512"",""item_order"": 1,""required"": true,""enabled"": true}]}";
Form tstfrm = JsonConvert.DeserializeObject<Form>(str);
MainPage = new FillForm(tstfrm); 

But when I run this code, it gives me the error System.ArgumentNullException: Value cannot be null. Parameter name: type even when I don't have any fields named "type" in my JSON object!

What is the problem in my code, and how can I solve it?

Stephane Delcroix
  • 16,134
  • 5
  • 57
  • 85
Abozanona
  • 2,261
  • 1
  • 24
  • 60

1 Answers1

1

You need to ensure that the type "Hura.Models.Cells." + inputtype exists in your currently executing assembly or in mscorlib.dll. If it doesn't, you have to specify the assembly name. (See here.)

In your example code the requested type name is Hura.Models.Cells.text which doesn't exist and therefore returns a null to the type parameter of the Activator.CreateInstance.

Maybe a null check will be enough but it depends on how you want to deal with that kind of situation.

Community
  • 1
  • 1
haindl
  • 3,111
  • 2
  • 25
  • 31
  • Your first answer before editing was the correct answer, Thank you vary much! The problem was in the classes namespace. I made a typo by mistake in the `inputtype` variable value and now it's working fine. You saved my live!! thanks. – Abozanona Oct 08 '16 at 22:31
  • 1
    I did a rollback of my edit to correctly reflect your situation. You're very welcome! Glad I could help you. :-) (And if my answer was helpful, you can of course upvote it too.) – haindl Oct 08 '16 at 22:35