当前位置: 首页 > 知识库问答 >
问题:

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

滕项明
2023-03-14

几个小时后,我发现问题与day.setIcon()有关。我得出这个结论是因为尽管调用了该方法,但mIcon的值没有改变。

java.lang.NullPointerException:尝试对teamtreehouse . com . my application . weather . forecast . geticoid(forecast . Java:39)处teamtreehouse . com . my application . weather . day . geticoid(day . Java:68)处的空对象引用调用虚方法“boolean Java . lang . string . equals(Java . object)”

第39行是:

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

第68行是:

return Forecast.getIconId(mIcon);

主活动类

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个答案

莫英卓
2023-03-14

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

笪栋
2023-03-14

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

 类似资料:
  • 我如何修复这个程序我使用Java Spring MVC?我想在数据库中输入包含日期的数据,但它显示错误。我该怎么修好它? 这是我在实体中的代码 字段“Tanggal”上对象“transaksipenjualan”中的字段错误:拒绝值[2019-07-15];代码[typemismatch.transaksipenjualan.tanggal,typemismatch.tanggal,typemis

  • 我得到这个错误: 命令:delete Number:1 java。sql。SQLException:未绑定到语句的值 我该怎么解决这个问题?是我漏掉了什么还是语法错了? Java 这应该只是从列表中删除了任务。

  • 我正在尝试制作一个可滚动的选项卡,但当我在我的片段布局中设置一个视图时,应用程序开始出现错误,因为错误inflating类为空。它在未添加视图时工作,但当我在布局中添加视图时,它会给我错误。我也按此处给出的那样添加了类,但它不起作用。我如何在这里修复这个问题是我的片段代码 片段包名称为包com。实例尼莱。另一个盒子。碎片片段名称歌曲。我怎样才能解决这个问题?错误 D/dalvikvm:GC_并发释

  • FileLoaderImportCircularReferenceException:在“/app/config/routing_dev.yml”(“/app/config/routing_dev.yml”)中检测到循环引用 我正在努力做到这一点:http://symfony.com/doc/current/cookbook/routing/custom_route_loader.html#mor

  • 我正试图使用此代码从消费者事务评论网站收集评论。但我不断地出错,特别是在dateElements中 这就是我得到的错误 回溯(最后一次调用):文件“C:/Users/Sara Jitkresorn/PycharmProjects/untitled/venv/caffirs.py”,第37行,jsonData=json。加载(dateElements)文件“C:\Users\Sara-Jitkres