Object Reference not set to an instance of an Object. - Json

Asked

Viewed 636 times

0

Follows code:

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.

Error occurs on line: model.properties.name.type = "cc";

3 answers

1

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

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

You can instill in the hand like this, or in the example class constructor, you put to instill the 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();
    }
}

1


Its classes have properties that are complex (classes) then they need to be instantiated, so that you have access to their innermost 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 builder of Hobbies instantiate Items, in the builder of Properties is instantiated Hobbies and Name and finally within the builder Example is instantiated Properties.

Online Project

If by chance do not want to do so, after instantiating Example if you can instantiate the classes as well, but I believe that the form I gave you is ideal, if you are going to use all this code, an example with another way to 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

  • 1

    Now I understand where I went wrong, thank you Virgilio :)

0

Wouldn’t be a mistake 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 as Properties.

I understand, I analyzed the code and I realized that you’re trying to access properties that don’t exist.

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

Note the above code. On the error line, the Properties class does not have the type property in the name property. It wouldn’t just be model.properties.type = "cc"?

  • "Json.Propertie" does not contain a definition for "type"

  • Sorry, it would be model.type;

  • It didn’t work, thanks for the help

  • For nothing! Whenever you need!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.