Object reference not set to an instance of an object. - Json

0

Follow the code below:

public class Name
    {
        public string type { get; set; }
    }

    public class Items
    {
        public string type { get; set; }
    }

    public class Hobbies
    {
        public string type { get; set; }
        public Items items { get; set; }
    }

    public class Properties
    {
        public Name name { get; set; }
        public Hobbies hobbies { get; set; }
    }

    public class Example
    {
        public string description { get; set; }
        public string type { get; set; }
        public Properties properties { get; set; }
    }

I'm trying to serialize like this:

Example model = new Example();
model.properties.name.type = "cc";
var data = JsonConvert.SerializeObject(model);

I get error:

  

Object reference not set to an instance of an object.

The error occurs on the line: model.properties.name.type = "cc";

    
asked by anonymous 07.04.2018 / 01:48

3 answers

1

Your classes have properties that are of complex type ( classes ) so they need to be instantiated, so you have access to their most internal properties, look at the changes:

public class Name
{
    public string type { get; set; }
}

public class Items
{   
    public string type { get; set; }
}

public class Hobbies
{
    public Hobbies()
    {
        items = new Items();
    }
    public string type { get; set; }
    public Items items { get; set; }
}

public class Properties
{
    public Properties()
    {
        hobbies = new Hobbies();
        name = new Name(); 
    }
    public Name name { get; set; }
    public Hobbies hobbies { get; set; }
}

public class Example
{
    public Example()
    {
        properties = new Properties();
    }
    public string description { get; set; }
    public string type { get; set; }
    public Properties properties { get; set; }
}

The sequence would be in the constructor of Hobbies instanciar Items , in the constructor of Properties is instantiated Hobbies and Name and finally in of the constructor Example is instantiated Properties .

ONLine Project

If you do not want to do so, after instantiating Example you can instantiate the classes as well, but I believe that the form I gave you is ideal, if you use all this code, an example with another form of work with these instances:

using Newtonsoft.Json;

public class Name
{
    public string type { get; set; }
}

public class Items
{   
    public string type { get; set; }
}

public class Hobbies
{
    public string type { get; set; }
    public Items items { get; set; }
}

public class Properties
{   
    public Name name { get; set; }
    public Hobbies hobbies { get; set; }
}

public class Example
{
    public string description { get; set; }
    public string type { get; set; }
    public Properties properties { get; set; }
}

public class Program
{
    public static void Main()
    {
        Example model = new Example();
        model.properties = new Properties();
        model.properties.name = new Name();
        model.properties.hobbies = new Hobbies();
        model.properties.hobbies.items = new Items();
        model.properties.name.type = "cc";
        var data = JsonConvert.SerializeObject(model);
        System.Console.WriteLine(data);
    }
}

OnLine Project

    
07.04.2018 / 02:23
1

The problem is because within the class Example, you have another class with the name of properties, and you just instantiated the Example class, but you would also have to instantiate the properties later to be able to use it, for example:

Example model = new Example();
model.properties = new Properties();
model.properties.name.type = "cc";
var data = JsonConvert.SerializeObject(model);

You can instantiate in the hand like this, or else in the example class constructor, you put to instantiate properties, you can leave your Example class as below and your code will work:

public class Example
{
    public string description { get; set; }
    public string type { get; set; }
    public Properties properties { get; set; }

    public Example()
    {
       properties = new Properties();
    }
}
    
07.04.2018 / 16:36
0

Would not it be an error in the class name?

public class Propertie
{
    public Name name { get; set; }
    public Hobbies hobbies { get; set; }
}

It is declared as Propertie, but you try to access it as Properties.

I understood, I analyzed the code and realized that you are trying to access properties that do not exist.

Example model = new Example();
model.properties.name.type = "cc";
var data = JsonConvert.SerializeObject(model);

Note the code above. In the error line, the Properties class does not have the type property in the name property. Would not it just be model.properties.type="cc"?

    
07.04.2018 / 02:01