Skip to content

NullReferenceException: Object reference not set to an instance of an object If value is Null #122

@corbinyo

Description

@corbinyo

When using:

public JsonData itemData;

itemData = JsonMapper.ToObject("[" + myjson.text.Trim() + "]");

 void ConstructItemDatabase()
        {
            for (int i = 0; i < itemData[0]["listings"].Count; i++)
            { 
                Debug.Log("Count all the listings FOR BROWSE : " + itemData);

                Debug.Log("log" + itemData[0]["listings"][i]["name"]);

                Debug.Log("token" + itemData[0]["listings"][i]["token"]);

                Debug.Log("SID" + itemData[0]["listings"][i]["sequenceId"]);

                Debug.Log("thumbnail" + itemData[0]["listings"][i]["thumbnailImageUrl"]);

                database.Add(new Art(
                (int)itemData[0]["listings"][i]["sequenceId"],
                (int)itemData[0]["listings"][i]["artId"],
                itemData[0]["listings"][i]["token"].ToString(),
                itemData[0]["listings"][i]["name"].ToString(),
                itemData[0]["listings"][i]["thumbnailImageUrl"].ToString()));
            }
        }
    }

with the Json:

{
  "searchId": null,
  "listings": [
    {
      "sequenceId": 0,
      "artId": 75,
      "token": null,
      "name": "Frank",
      "thumbnailImageUrl": "https://imymages/1.jpg"
    },
    {
      "sequenceId": 1,
      "artId": 15,
      "token": "null",
      "name": "peep",
      "thumbnailImageUrl": "myimages/2.jpg"
    }
  ]

The Null value from "Token" from the 1st listing object returns NullReferenceException: Object reference not set to an instance of an object

Why Can I not have null values?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions