WeatherLib icon indicating copy to clipboard operation
WeatherLib copied to clipboard

NullPointerException

Open athkalia opened this issue 9 years ago • 4 comments

Hello, I am getting a NullPointerException from your library. Unfortunately the code is obfuscated so I can't trace it down 100%

java.lang.NullPointerException: Attempt to invoke virtual method 'double org.json.JSONObject.optDouble(java.lang.String)' on a null object reference
    at com.survivingwithandroid.weather.lib.provider.openweathermap.OpenweathermapProvider.getCurrentCondition(SourceFile:1516)
    at bno.onResponse(SourceFile:380)
    at bkr.execute(SourceFile:162)
    at com.squareup.okhttp.internal.NamedRunnable.run(SourceFile:33)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

Edit:

We are getting the exception because the response is a weird openweathermap response, error 401 that can't be parsed by your library. The easiest way to fix this is to catch NullpointExceptions and return an empty weather object.

The json we receive is: {"cod":401, "message": "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info."}

athkalia avatar Dec 28 '15 21:12 athkalia

Note:

In their json respond api here http://openweathermap.org/current#current_JSON there's no "gust" field at all ! Maybe you should just remove that ?

Cheers

athkalia avatar Dec 28 '15 22:12 athkalia

@athkalia So this is still an issue. How did you deal with it?

Seichis avatar Apr 24 '16 18:04 Seichis

I made a fork that catches all exceptions and forwards them accordingly in the already existing onError listeners. It's this one. https://github.com/athkalia/WeatherLib . This is the only change: https://github.com/athkalia/WeatherLib/commit/44a43e0b5c339e820521bdf538883733f38906fe

athkalia avatar Apr 24 '16 21:04 athkalia

The same problem happens to me! How can I handle this?

tsakmalis avatar Sep 20 '16 12:09 tsakmalis