survivingwithandroid / WeatherLib

Android Weather Library: android weather lib to develop weather based app fast and easily
http://survivingwithandroid.github.io/WeatherLib/
640 stars 170 forks source link

NullPointerException #44

Open athkalia opened 8 years ago

athkalia commented 8 years ago

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 commented 8 years ago

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

Seichis commented 8 years ago

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

athkalia commented 8 years ago

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

tsakmalis commented 8 years ago

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