2

I'm trying to parse the JSON from the following API: https://opentdb.com/api.php?amount=1

but when I'm trying to get the question value, I get the following error:

Exception in thread "main" java.lang.NullPointerException

I use this code:

public static void main(String[] args) throws IOException {
    String question;
    String sURL = "https://opentdb.com/api.php?amount=1"; //just a string

    // Connect to the URL using java's native library
    URL url = new URL(sURL);
    URLConnection request = url.openConnection();
    request.connect();

    // Convert to a JSON object to print data
    JsonParser jp = new JsonParser(); //from gson
    JsonElement root = jp.parse(new InputStreamReader((InputStream) request.getContent())); //Convert the input stream to a json element
    JsonObject rootobj = root.getAsJsonObject(); //May be an array, may be an object.
    question = rootobj.get("question").getAsString(); //grab the question

}

Hope someone can tell me what I am doing wrong.

Thanks in advance!

3
  • 1
    what line is the NullPointerException? did you try to debug the program? Commented Jun 11, 2018 at 15:15
  • 1
    IT is for line 10: "question = rootobj.get("question").getAsString();" Commented Jun 11, 2018 at 15:19
  • 1
    so probably rootobj.get("question") is returning null, separate this line into 2 lines, Commented Jun 11, 2018 at 15:22

3 Answers 3

7

When I look at the JSON you are trying to interpret, I get:

{
    "response_code": 0,
    "results":[
        {
            "category": "Entertainment: Board Games",
            "type": "multiple",
            "difficulty": "medium",
            "question": "Who is the main character in the VHS tape included in the board game Nightmare?",
            "correct_answer": "The Gatekeeper",
            "incorrect_answers":["The Kryptkeeper","The Monster","The Nightmare"]
        }
    ]
}

This JSON doesn't contain a root member "question". This makes rootobj.get("question") return null, and therefore calling getAsString on it throws the NullPointerException.

So instead of rootobj.get("question"), you would have to walk through the hierarchy: "results" -> first array member -> "question":

rootobj.getAsJsonArray("result").getAsJsonObject(0).get("question")
Sign up to request clarification or add additional context in comments.

Comments

2

Try this one

question = rootobj.getAsJsonArray("results").get(0).getAsJsonObject().get("question").getAsString();

Comments

2

The JSON has no direct "question" field. call question = rootobj.get("result").get(0).get("question").getAsString(); instead

2 Comments

Hmm just tryed your snippet but now is says "Cannot resolve method 'get(int)'"
That is right, your are using Gson parser and it has diffrent syntax. Looks like @Daniel Przybylowski has the right syntax for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.