複数の時間が経過した後、私は問題がday.setIcon()
で何かをしなければならないということを指摘しました。メソッドを呼び出すにもかかわらず、mIcon
の値は変更されていないので、私はこの結論に達しました。コード内のバグ:nullオブジェクト参照でのNullPointerException、どのように修正する必要がありますか?
java.lang.NullPointerExceptionが: teamtreehouse.com.myapplicationでヌルオブジェクト 参照仮想方法 'ブールjava.lang.String.equals(java.lang.Object上位)' を呼び出すための試み。 teamtreehouse.com.myapplication.weather.Day.getIconIdでweather.Forecast.getIconId(Forecast.java:39) (Day.java:68)
ライン39は以下のとおりです。
if (iconString.equals("clear-day")) {
ライン68は、次のとおりです。
return Forecast.getIconId(mIcon);
MainActivityクラス
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;
}
}
[NullReferenceExceptionとは何ですか?それを修正するにはどうすればいいですか?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix -it) – nhouser9
@ nhouser9あなたは間違った重複を選んだと思います。 –
[NullPointerExceptionとは何か、それを修正する方法は?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) ) –