joshuar500 joshuar500 - 6 months ago 27
JSON Question

Gson not loading in properly - Null object reference

I'm trying to load in a local file into my Android application using Gson. Loading in the file works just fine. Here is what the Json looks like:

{"heroes" : [
{
"hero": {
"name": "Hanzo",
"role": "Offense",
"abilities": {
"primary": "left click",
"secondary": "right click",
"ultimate": "fucking dragons"
},
"strongAgainst": [
"Bastion",
"Mercy"
],
"weakAgainst": [
"Genji",
"Tracer"
]
},
"hero": {
"name": "Torbjorn",
"role": "Defense",
"abilities": {
"primary": "left click",
"secondary": "right click",
"ultimate": "lava bastard"
},
"strongAgainst": [
"Lucio",
"Mercy"
],
"weak_against": [
"Widowmaker",
"Junkrat"
]
}
}
]}


I have created 3 POJOs with getters and setters like so:

public final class Heroes {
private List<Hero> hero;
}

public class Hero {
private HeroAttr hero;
}

public class HeroAttr {
private String name;
private String role;
private Abilities abilities;
private List<String> strongAgainst;
private List<String> weakAgainst;
}

public class Abilities {
private String primary;
private String secondary;
private String ultimate;
}


And I load in using Gson like so:

Gson gson = new GsonBuilder().create();
Heroes h = gson.fromJson(byteArrayOutputStream.toString(), Heroes.class);


And whenever I try to get the size or print something from
h
it gives me a null object reference and I am unable to get anything from the arraylist.

I've even tried making the json file only an array and using

Hero[] h = gson.fromJson(byteArrayOutputStream.toString(), Hero[].class);


but I get the same issue -- null object reference.

Any ideas? Thanks.

Answer

UPDATED

You got a problem in your JSON:

{
    "hero": {
        "name": "Hanzo",
        "role": "Offense",
        "abilities": {
            "primary": "left click",
            "secondary": "right click",
            "ultimate": "fucking dragons"
        },
        "strongAgainst": [
            "Bastion",
            "Mercy"
        ],
        "weakAgainst": [
            "Genji",
            "Tracer"
        ]
    },
    "hero": {
        "name": "Torbjorn",
        "role": "Defense",
        "abilities": {
            "primary": "left click",
            "secondary": "right click",
            "ultimate": "lava bastard"
        },
        "strongAgainst": [
            "Lucio",
            "Mercy"
        ],
        "weak_against": [
            "Widowmaker",
            "Junkrat"
        ]
    }
}

This is not sane. An object defining hero property twice.

The full version should be:

{
    "heroes" : [
        {
            "name": "Hanzo",
            "role": "Offense",
            "abilities": {
                "primary": "left click",
                "secondary": "right click",
                "ultimate": "fucking dragons"
            },
            "strongAgainst": [
                "Bastion",
                "Mercy"
            ],
            "weakAgainst": [
                "Genji",
                "Tracer"
            ]
        },
        {
            "name": "Torbjorn",
            "role": "Defense",
            "abilities": {
                "primary": "left click",
                "secondary": "right click",
                "ultimate": "lava bastard"
            },
            "strongAgainst": [
                "Lucio",
                "Mercy"
            ],
            "weak_against": [
                "Widowmaker",
                "Junkrat"
            ]
        }
    ]
}

Full code (back to three POJOs):

import com.google.gson.Gson;

import java.util.List;

public class Test {
    String json = "{\n" +
                  "    \"heroes\" : [\n" +
                  "        {\n" +
                  "            \"name\": \"Hanzo\",\n" +
                  "            \"role\": \"Offense\",\n" +
                  "            \"abilities\": {\n" +
                  "                \"primary\": \"left click\",\n" +
                  "                \"secondary\": \"right click\",\n" +
                  "                \"ultimate\": \"fucking dragons\"\n" +
                  "            },\n" +
                  "            \"strongAgainst\": [\n" +
                  "                \"Bastion\",\n" +
                  "                \"Mercy\"\n" +
                  "            ],\n" +
                  "            \"weakAgainst\": [\n" +
                  "                \"Genji\",\n" +
                  "                \"Tracer\"\n" +
                  "            ]\n" +
                  "        },\n" +
                  "        {\n" +
                  "            \"name\": \"Torbjorn\",\n" +
                  "            \"role\": \"Defense\",\n" +
                  "            \"abilities\": {\n" +
                  "                \"primary\": \"left click\",\n" +
                  "                \"secondary\": \"right click\",\n" +
                  "                \"ultimate\": \"lava bastard\"\n" +
                  "            },\n" +
                  "            \"strongAgainst\": [\n" +
                  "                \"Lucio\",\n" +
                  "                \"Mercy\"\n" +
                  "            ],\n" +
                  "            \"weak_against\": [\n" +
                  "                \"Widowmaker\",\n" +
                  "                \"Junkrat\"\n" +
                  "            ]\n" +
                  "        }\n" +
                  "    ]\n" +
                  "}";

    static class Heroes {
        public List<Hero> heroes;
    }

    static class Hero {
        public String name;
        public String role;
        public Abilities abilities;
        public List<String> strongAgainst;
        public List<String> weakAgainst;
    }

    static class Abilities {
        public String primary;
        public String secondary;
        public String ultimate;
    }

    void go() {
        Gson gson = new Gson();
        Heroes h = gson.fromJson(json, Heroes.class);
        System.out.println(h.heroes.size());
        System.out.println(h.heroes.get(0).name);
    }

    public static void main(String[] args) {
        new Test().go();
    }
}