0

After multiple hours, I've pin-point that the problem has to do something with with day.setIcon(). I came to this conclusion because mIcon's value is not changed in spite invoking the method.

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference at teamtreehouse.com.myapplication.weather.Forecast.getIconId(Forecast.java:39) at teamtreehouse.com.myapplication.weather.Day.getIconId(Day.java:68)

Line 39 is:

if (iconString.equals("clear-day")) {

Line 68 is:

return Forecast.getIconId(mIcon);

MainActivity Class

private Day[] getDailyForecast(String jsonData) throws JSONException {
    JSONObject forecast = new JSONObject(jsonData);
    String timezone = forecast.getString("timezone");
    JSONObject daily = forecast.getJSONObject("daily");
    JSONArray data = daily.getJSONArray("data");

Day[] days = new Day[data.length()];

for (int i = 0; i < data.length(); i++) {
    JSONObject jsonDay = data.getJSONObject(i);
    Day day = new Day();
    day.setIcon("snow"); //not changing mIcon


    day.setSummary(jsonDay.getString("summary"));

    day.setTemperatureMax(jsonDay.getDouble("temperatureMax"));
    day.setTime(jsonDay.getLong("time"));
    day.setTimezone(timezone);

    days[i] = day;
}

return days;

}

Day Class

package teamtreehouse.com.myapplication.weather;

import android.os.Parcel;
import android.os.Parcelable;
import android.util.Log;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/**
 * Created by Owner on 2016-09-14.
 */
public class Day implements Parcelable {
    private long mTime;
    private String mSummary;
    private double mTemperatureMax;
    private String mIcon;
    private String mTimezone;


    public long getTime() {
        return mTime;
    }

    public void setTime(long time) {
        mTime = time;
        Log.d("mTime", "mTime");
    }

    public String getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }

    public int getTemperatureMax() {
        return (int) Math.round(mTemperatureMax);
    }

    public void setTemperatureMax(double temperatureMax) {
        mTemperatureMax = temperatureMax;
    }

    public String getIcon() {
        return mIcon;
    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public String getTimezone() {
        return mTimezone;
    }

    public void setTimezone(String timezone) {
        mTimezone = timezone;
    }

    public int getIconId() {
        return Forecast.getIconId(mIcon);
    }

    public String getDaysOfTheWeek(){
        SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
        formatter.setTimeZone(TimeZone.getTimeZone(mTimezone));
        Date dateTime = new Date(mTime * 1000);
        return formatter.format(dateTime);
    }
 }

Forecast class

package teamtreehouse.com.myapplication.weather;

import teamtreehouse.com.myapplication.R;

/**
 * Created by Owner on 2016-09-14.
 */
public class Forecast {
    private Current mCurrent;
    private Hour[] mHourlyForecast;
    private Day[]mDailyForecast;

    public Current getCurrent() {
        return mCurrent;
    }

    public void setCurrent(Current current) {
        mCurrent = current;
    }

    public Hour[] getHourlyForecast() {
        return mHourlyForecast;
    }

    public void setHourlyForecast(Hour[] hourlyForecast) {
        mHourlyForecast = hourlyForecast;
    }

    public Day[] getDailyForecast() {
        return mDailyForecast;
    }

    public void setDailyForecast(Day[] dailyForecast) {
        mDailyForecast = dailyForecast;
    }

    public static int getIconId(String iconString){
        int iconId = R.drawable.clear_day;
        if (iconString.equals("clear-day")) { //you can use switch-case statement
            iconId = R.drawable.clear_day;
        }
        else if (iconString.equals("clear-night")) {
            iconId = R.drawable.clear_night;
        }
        else if (iconString.equals("rain")) {
            iconId = R.drawable.rain;
        }
        else if (iconString.equals("snow")) {
            iconId = R.drawable.snow;
        }
        else if (iconString.equals("sleet")) {
            iconId = R.drawable.sleet;
        }
        else if (iconString.equals("wind")) {
            iconId = R.drawable.wind;
        }
        else if (iconString.equals("fog")) {
            iconId = R.drawable.fog;
        }
        else if (iconString.equals("cloudy")) {
            iconId = R.drawable.cloudy;
        }
        else if (iconString.equals("partly-cloudy-day")) {
            iconId = R.drawable.partly_cloudy;
        }
        else if (iconString.equals("partly-cloudy-night")) {
            iconId = R.drawable.cloudy_night;
        }
        return iconId;
    }
}
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – nhouser9 Sep 20 '16 at 02:01
  • @nhouser9 I think you chose the wrong duplicate. – Jonny Henly Sep 20 '16 at 02:02
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jonny Henly Sep 20 '16 at 02:03
  • Well, the stack trace says that it happens on line 39 of Forecast.java. Which line is that? – Dawood ibn Kareem Sep 20 '16 at 02:05
  • My bad, line 39 is: if (iconString.equals("clear-day")) { – Matthew Francis Sep 20 '16 at 02:07
  • @John Henly, I believe mIcon is set to null, the problem is I'm not sure why it is null. – Matthew Francis Sep 20 '16 at 02:11
  • 1
    OK, so the message seems to confirm your suspicion that `mIcon` is not getting set. But there's no reason here why that would be the case. I'm wondering whether the code that you're actually running is not the same as the source that's shown here, for some reason. Have you tried deleting all the .class files and rebuilding them? – Dawood ibn Kareem Sep 20 '16 at 02:11
  • From day.seticon to string.equals... somewhere along the way the variable is getting lost and logging each step will tell you. – Nerdy Bunz Sep 20 '16 at 02:20
  • Try throw in `setIcon` when the argument is null. That will either tell you where it get the null value, or you know the method was never called. – xiaofeng.li Sep 20 '16 at 02:20
  • Have you log data.length()? Can you sure you receive data and decoding in the right way? – i_A_mok Sep 20 '16 at 04:52

2 Answers2

0

iconString is null. This means mIcon is null. I don't see where you initialize this variable.

Code-Apprentice
  • 69,701
  • 17
  • 115
  • 226
  • day.setIcon("snow") is supposed to make mIcon set to "snow", than iconString is suppose to be the mIcon. In theory, didnt I initialize iconString? – Matthew Francis Sep 20 '16 at 02:17
  • 1
    @MatthewFrancis You need to be sure this happens before using iconString. You should use the debugged to find out what is happening. – Code-Apprentice Sep 20 '16 at 02:43
-1

I really feel bad since I still can't write comments. I would suggest passing the mIcon in the constructor and that way you will be sure you wont get null pointer. And it's possible that getIconId is called before the setIcon.