Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSølvi Qorda
14,194 PointsGetting a fatal runtime error for OKHttp, and my log doesn't appear to show any retrieval of Forecast data.
Hi community!
I'm still struggling to retrieve any data, and my app crashes on startup. Any ideas much appreciated!
E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{qorda_projects....}: java.lang.NullPointerException
Sølvi Qorda
14,194 PointsHi Gavin,
thanks for your answer - so I'm looking for a null pointer exception at the lines listed below? What should I try to change?
Kind regards
FATAL EXCEPTION: OkHttp Dispatcher
Process: qorda_projects.stormzystormz, PID: 3656
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at qorda_projects.stormzystormz.CurrentWeather.<init>(CurrentWeather.java:50)
at qorda_projects.stormzystormz.MainActivity.getCurrentDetails(MainActivity.java:179)
at qorda_projects.stormzystormz.MainActivity.access$400(MainActivity.java:32)
at qorda_projects.stormzystormz.MainActivity$2.onResponse(MainActivity.java:119)
at com.squareup.okhttp.Call$AsyncCall.execute(Call.java:168)
at com.squareup.okhttp.internal.NamedRunnable.run(NamedRunnable.java: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)
(apologies, it's formatted so that you have to scroll right to see the main body of text)
1 Answer
Gavin Ralston
28,770 PointsOk so, remember the stack trace is "upside down" showing from the bottom up a list of methods that called the next one, which called the next one... til it broke down. I edited it down a little so it'd fit.
java.lang.NullPointerException: [Tried to do a string comparison on nothing] 'boolean
java.lang.String.equals(java.lang.Object)' on a null object reference
at qorda_projects.stormzystormz.CurrentWeather.<init>(CurrentWeather.java:50)
at qorda_projects.stormzystormz.MainActivity.getCurrentDetails(MainActivity.java:179)
at qorda_projects.stormzystormz.MainActivity.access$400(MainActivity.java:32)
at qorda_projects.stormzystormz.MainActivity$2.onResponse(MainActivity.java:119)
So where I'd start is around line 119 in MainActivity (which looks like it's in the onResponse() callback) then follow along and watch what's happening.
You wind up calling getCurrentDetails() and then you create a new CurrentWeather object, and then somewhere you're messing around with a string that doesn't exist. (That's where I put my "summary" of the error message in brackets in the stack trace above)
You can post the code if you want from MainActivity if you want extra eyes on it, but I think the problem probably exists in the CurrentWeather class.
If I were a betting man, I'd look into the "if/else" statements for the getIconId() method, where the tutorial had you doing a lot of checking out the icon type strings (like clear-day, or cloudy-night) in order to properly set the iconID from your project resources.
If you want to confirm that, try setting a debug point right at the spot where you call .setIcon() on the CurrentWeather object inside your getCurrentDetails() method and then step through and watch the variables change... or watch your code break, and try to figure out what the culprit is. Or if you want to check it a little closer, set the debug point one line into the CurrentWeather.getIconId() method and watch as it checks each icon name value.
Gavin Ralston
28,770 PointsGavin Ralston
28,770 PointsUsually there's a few lines after this that say things like "at (line number)" and maybe even the method which is throwing the error.
That'll help narrow it down a bit. Null pointer exceptions are annoying, but it might help to see what you're trying to point at to see what's missing. Then you can hunt down the culprit (the object that wasn't instantiated, the array index that was out of bounds, etc)