代码中的错误:null对象引用上的NullPointerException,我该如何修复它?

时间:2016-09-20 02:00:11

标签: java android debugging nullpointerexception

经过多个小时后,我已经指出问题必须与day.setIcon()有关。我得出了这个结论,因为尽管调用该方法,mIcon的值仍未改变。

  

java.lang.NullPointerException:尝试调用虚方法   ' boolean java.lang.String.equals(java.lang.Object)'在null对象上   参考                                                                                      在   teamtreehouse.com.myapplication.weather.Forecast.getIconId(Forecast.java:39)                                                                                      at teamtreehouse.com.myapplication.weather.Day.getIconId(Day.java:68)

第39行是:

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

第68行是:

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;

}

日级

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);
    }
 }

预测课程

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;
    }
}

2 个答案:

答案 0 :(得分:0)

iconString为null。这意味着mIcon为null。我不知道你在哪里初始化这个变量。

答案 1 :(得分:-1)

我真的很难过,因为我仍然无法写评论。我建议在构造函数中传递mIcon,这样你就可以确定你不会得到空指针。并且可能在setIcon之前调用getIconId。