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 trial

Android Build a Weather App (2015) Hooking Up the Model to the View Setting the Weather Icon

amir baldiga
amir baldiga
2,530 Points

when i run the app i can't get normal time display

when i try to run the app i get this message

java.lang.NullPointerException: id == null

my CurrentWeather method:

  public String getRealTime(){
    SimpleDateFormat format = new SimpleDateFormat("k:mm", Locale.US);
    format.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
    Date day = new Date(getTime() * 1000);


    return  format.format(day);
}

and the call in the Main Activity:

mTimeLabel.setText("At" + mWeather.getRealTime() + "");

2 Answers

Hi there,

What is pointing to null when you run your code?

I've got a version of this app that is a little more developed because I finished the course - a long time ago, hence my lack of recollection!!

So, the getRealTime() method (my code has getFormattedTime()) looks exactly like what you've got, so that's not where the problem is.

I suspect the null is coming from the JSON - can you add a breakpoint near where this is called in MainActivity? The mWeather variable needs to hold something; adding a breakpoint would check that. In my code, it creates an instance of the Forecast class then sets a few labels lik e mTemperatureLabel then mTimeLabel. We need to check that mWeather holds something meaningful - the breakpoint will do that.

Steve.

amir baldiga
amir baldiga
2,530 Points

got it figured by using a simple if() in the getRealTime to disallow a situation of null to be returned

thank you very much for helping me again :)

That's a good fix, yes. You may run into problems with that further in the project. Feel free to ask if you need any help !

Steve.