1

I'm learning java recently and when I wanted to parse the data from a weather website, I got this error and I still can't figure it out, can anybody tell why I got this error: java.lang.NullPointerException ?

public class main {
    public static void main(String []args) {
        try {
            URL url = new URL("http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=caa2f68a7b2b43a09c115021171404&format=json&q=atlanta&date=2015-07-20&tp=24");
            InputStream is = url.openStream();
            JsonReader rdr = Json.createReader(is);
            JsonObject obj = rdr.readObject();
            JsonArray data = obj.getJsonArray("weather");
            JsonObject objectOfData = data.getJsonObject(0);
            System.out.println(objectOfData.getString("date"));
        } catch (Exception ex){
            System.out.println(ex);
        }

    }
}

Here is the data parsed from Postman:

enter image description here

Here is the trace log:

enter image description here

Here is the picture of line 19

enter image description here

user7341005
  • 285
  • 1
  • 3
  • 13

2 Answers2

1

Part 1: Error with Callback

I checked your URL in my browser and it appears you have the callback parameter set to wrap the json with a request callback which looks like this:

request({"data": .... })

The parser is getting hung up on the first character which it doesn't recognize as proper json.

Try this URL instead:

http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=caa2f68a7b2b43a09c115021171404&format=json&q=atlanta&date=2015-07-20&tp=24

Part 2: Null Pointer Exception

The json is being parsed out of order. You needed to create an object from the root element "data" before accessing the array.

        try {
            URL url = new URL("http://api.worldweatheronline.com/premium/v1/past-weather.ashx?key=caa2f68a7b2b43a09c115021171404&format=json&q=atlanta&date=2015-07-20&tp=24");
            InputStream is = url.openStream();
            JsonReader rdr = Json.createReader(is);
            JsonObject obj = rdr.readObject();
            JsonObject objectOfData = (JsonObject) obj.get("data");
            JsonArray data = objectOfData.getJsonArray("weather");
            JsonObject a = data.getJsonObject(0);
            System.out.println(a.getString("date"));

        } catch (Exception ex){
            System.out.println(ex);
        }
BobMaertz
  • 36
  • 4
1

You may print the stack trace in the catch method, and find on which line there is error and act accordingly.

Deepesh Choudhary
  • 660
  • 1
  • 8
  • 17
  • Thank you, I've renewed it. – user7341005 Apr 14 '17 at 04:30
  • The trace log says the error is at line number 19, but in the code you have posted, there are less that 19 lines. You may edit your post(post the full code) or tell the line on which there is error. – Deepesh Choudhary Apr 14 '17 at 04:43
  • I've renewed it and I found a strange thing that when I write code in line 19, when I add the dot "***.***" after 'data' I found I can only getJsonObject with a Int other than a String – user7341005 Apr 14 '17 at 05:09
  • At line 19, there can be error only in the variable "data". So the problem is that either the variable is not initialized properly or it has been given the value null. (Sorry to say, but I don't know much about json, but I know java very well, so I can confidently say the problem is in the variable. – Deepesh Choudhary Apr 14 '17 at 05:34