-1

i am trying to get the "definitions" inside the "senses" array how ever there's an object reference error. I created a class and i can only get the values up to "results" array only e.g(id,language,text and word) but i cant access the "lexicalEntries","entries" and "senses" arrays and there values because of the error.

This is my JSON

"id":"bird",
"metadata":{
"operation":"retrieve",
"provider":"Oxford University Press",
"schema":"RetrieveEntry"
},
"results":[
{
"id":"bird",
"language":"en-gb",
"lexicalEntries":[
{
"entries":[
{
"senses":[
{
"definitions":[
"a warm-blooded egg-laying vertebrate animal distinguished by the possession of feathers, wings, a beak, and typically by being able to fly."
],
"id":"m_en_gbus0097360.006",
"subsenses":[]
},
{
"definitions":[
"a person of a specified kind or character"
],
"id":"m_en_gbus0097360.014"
},
{
"definitions":[
"a young woman or a girlfriend."
],
"id":"m_en_gbus0097360.016"
}
]
}
],
"language":"en-gb",
"lexicalCategory":{
"id":"noun",
"text":"Noun"
},
"text":"bird"
}
],
"type":"headword",
"word":"bird"
}
],
"word":"bird"
}

this is my class

 class WordDefinition
    {

        public RootObject rootObject { get; set; }
        public Metadata metadata { get; set; }
        public List<Result> results { get; set; }
        public List<LexicalEntry> lexicalEntries { get; set; }
        public List<Entry> entries { get; set; }
        public List<Sens> senses { get; set; }
        public List<Subsens> subsenses { get; set; }

        public LexicalCategory lexicalCategory { get; set; }

        public class Metadata
        {
            public string operation { get; set; }
            public string provider { get; set; }
            public string schema { get; set; }
        }

        public class Subsens
        {
            public List<string> definitions { get; set; }
            public string id { get; set; }
        }

        public class Sens
        {
            public List<string> definitions { get; set; }
            public string id { get; set; }
            public List<Subsens> subsenses { get; set; }
        }

        public class Entry
        {
            public List<Sens> senses { get; set; }
        }

        public class LexicalCategory
        {
            public string id { get; set; }
            public string text { get; set; }
        }

        public class LexicalEntry
        {
            public List<Entry> entries { get; set; }
            public string language { get; set; }
            public LexicalCategory lexicalCategory { get; set; }
            public string text { get; set; }
        }

        public class Result
        {
            public string id { get; set; }
            public string language { get; set; }
            public List<LexicalEntry> lexicalEntries { get; set; }
            public string type { get; set; }
            public string word { get; set; }
        }

        public class RootObject
        {
            public string id { get; set; }
            public Metadata metadata { get; set; }
            public List<Result> results { get; set; }
            public string word { get; set; }
        }
    }

and

var test = JsonConvert.DeserializeObject<WordDefinition>(jsonResponse);

foreach(var testing in test.senses)
{
    MessageBox.Show(testing.definitions[0].ToString());
}

I expect the output must be results.definitions[0]. but there's an error:

Object reference not set to an instance of an object error

...in json

marc_s
  • 675,133
  • 158
  • 1,253
  • 1,388
Dio
  • 1
  • 1
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp Aug 04 '19 at 21:33

1 Answers1

2

There are a few things going on here.

1) The JSON isn't valid, it should start with an { character, e.g.:

{
    "id": "bird",
    "metadata": {
        "operation": "retrieve",
        "provider": "Oxford University Press",
        "schema": "RetrieveEntry"
    },
    "results": [
        {
            "id": "bird",
            "language": "en-gb",
            "lexicalEntries": [
                {
                    "entries": [
                        {
                            "senses": [
                                {
                                    "definitions": [
                                        "a warm-blooded egg-laying vertebrate animal distinguished by the possession of feathers, wings, a beak, and typically by being able to fly."
                                    ],
                                    "id": "m_en_gbus0097360.006",
                                    "subsenses": []
                                },
                                {
                                    "definitions": [
                                        "a person of a specified kind or character"
                                    ],
                                    "id": "m_en_gbus0097360.014"
                                },
                                {
                                    "definitions": [
                                        "a young woman or a girlfriend."
                                    ],
                                    "id": "m_en_gbus0097360.016"
                                }
                            ]
                        }
                    ],
                    "language": "en-gb",
                    "lexicalCategory": {
                        "id": "noun",
                        "text": "Noun"
                    },
                    "text": "bird"
                }
            ],
            "type": "headword",
            "word": "bird"
        }
    ],
    "word": "bird"
}

2) The collections should be initialized in the class definitions, e.g.:

class WordDefinition
{

    public RootObject rootObject { get; set; }
    public Metadata metadata { get; set; }
    public List<Result> results { get; set; } = new List<Result>();
    public List<LexicalEntry> lexicalEntries { get; set; } = new List<LexicalEntry>();
    public List<Entry> entries { get; set; } = new List<Entry>();
    public List<Sens> senses { get; set; } = new List<Sens>();
    public List<Subsens> subsenses { get; set; } = new List<Subsens>();

    public LexicalCategory lexicalCategory { get; set; }

    public class Metadata
    {
        public string operation { get; set; }
        public string provider { get; set; }
        public string schema { get; set; }
    }

    public class Subsens
    {
        public List<string> definitions { get; set; } = new List<string>();
        public string id { get; set; }
    }

    public class Sens
    {
        public List<string> definitions { get; set; } = new List<string>();
        public string id { get; set; }
        public List<Subsens> subsenses { get; set; } = new List<Subsens>();
    }

    public class Entry
    {
        public List<Sens> senses { get; set; } = new List<Sens>();
    }

    public class LexicalCategory
    {
        public string id { get; set; }
        public string text { get; set; }
    }

    public class LexicalEntry
    {
        public List<Entry> entries { get; set; } = new List<Entry>();
        public string language { get; set; }
        public LexicalCategory lexicalCategory { get; set; }
        public string text { get; set; }
    }

    public class Result
    {
        public string id { get; set; }
        public string language { get; set; }
        public List<LexicalEntry> lexicalEntries { get; set; } = new List<LexicalEntry>();
        public string type { get; set; }
        public string word { get; set; }
    }

    public class RootObject
    {
        public string id { get; set; }
        public Metadata metadata { get; set; }
        public List<Result> results { get; set; } = new List<Result>();
        public string word { get; set; }
    }
}

And 3) The JSON doesn't have a senses element at the root level. Maybe you intended test.results[0].lexicalEntries[0].entries[0].senses instead? e.g.:

var test = JsonConvert.DeserializeObject<WordDefinition>(jsonResponse);
foreach (var testing in test.results[0].lexicalEntries[0].entries[0].senses)
{
    MessageBox.Show(testing.definitions[0].ToString());
}
AlwaysLearning
  • 4,396
  • 4
  • 21
  • 28