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
Josh Gold
12,207 PointsNullPointerException: id == null in Stormy
NullPointerException seems to be my personal nemesis in every app that I make. I am building the Stormy app, but mine is just called WeatherBuddy. It retrieves a few addional weather items, but that is not the issue here.
Launching the daily (5 day forecast) from MainActivity causes the NullPointerException. The current forecast and the hourly forecast work fine.
So I haven't been able to figure out yet what variable is not initialized. If you want me to post additonal classes from the app, I can do so.
Logcat:
11-25 17:37:57.095 13160-13160/com.joshbgold.WeatherBuddy E/AndroidRuntime:
FATAL EXCEPTION: main
java.lang.NullPointerException: id == null
at java.util.TimeZone.getTimeZone(TimeZone.java:305)
at com.joshbgold.WeatherBuddy.Day.getDayOfTheWeek(Day.java:68)
at com.joshbgold.WeatherBuddy.DayAdapter.getView(DayAdapter.java:66)
package com.joshbgold.WeatherBuddy;
import android.os.Parcel;
import android.os.Parcelable;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;
public class Day implements Parcelable{
private long mTime;
private String mSummary;
private String mIcon;
private String mTimezone;
private double mTemperatureMax;
private double mTemperatureMin;
private String mTimeZone;
private long mSunrise;
private long mSunset;
private double mMoonPhase;
public long getTime() {
return mTime;
}
public void setTime(long time) {
mTime = time;
}
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 getDayOfTheWeek() {
SimpleDateFormat formatter = new SimpleDateFormat("EEEE");
formatter.setTimeZone(TimeZone.getTimeZone(mTimezone));
Date dateTime = new Date(mTime * 1000);
return formatter.format(dateTime);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mTime);
dest.writeString(mSummary);
dest.writeDouble(mTemperatureMax);
dest.writeString(mIcon);
dest.writeString(mTimezone);
}
private Day(Parcel in) {
mTime = in.readLong();
mSummary = in.readString();
mTemperatureMax = in.readDouble();
mIcon = in.readString();
mTimezone = in.readString();
}
public Day() { }
public static final Creator<Day> CREATOR = new Creator<Day>() {
@Override
public Day createFromParcel(Parcel source) {
return new Day(source);
}
@Override
public Day[] newArray(int size) {
return new Day[size];
}
};
public String getMoonPhaseDescription() {
String phaseDescription;
if (mMoonPhase == 0) {
phaseDescription = "new moon";
} else if (mMoonPhase > 0 && mMoonPhase < 0.25) {
phaseDescription = "waxing crescent";
} else if (mMoonPhase == 0.25) {
phaseDescription = "first quarter";
} else if (mMoonPhase > 0.25 && mMoonPhase < 0.5) {
phaseDescription = "waxing gibbous";
} else if (mMoonPhase == 0.5) {
phaseDescription = "full moon";
} else if (mMoonPhase > 0.5 && mMoonPhase < 0.75) {
phaseDescription = "waning gibbous";
} else if (mMoonPhase == 0.75) {
phaseDescription = "last quarter";
} else if (mMoonPhase > 0.75 && mMoonPhase < 1) {
phaseDescription = "waning crescent";
} else phaseDescription = "--";
return phaseDescription;
}
public void setMoonPhase(double moonPhase) {
mMoonPhase = moonPhase;
}
public long getSunrise() {
return mSunrise;
}
public String getFormattedSunriseTime(){ //converts Unix time we receive from API, to human readable time
SimpleDateFormat formatter = new SimpleDateFormat("h:mm");
formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
Date dateTime = new Date(getSunrise() * 1000);
return formatter.format(dateTime);
}
public void setSunrise(long sunrise) {
mSunrise = sunrise;
}
public long getSunset() {
return mSunset;
}
public String getFormattedSunsetTime(){ //converts Unix time we receive from API, to human readable time
SimpleDateFormat formatter = new SimpleDateFormat("h:mm");
formatter.setTimeZone(TimeZone.getTimeZone(getTimeZone()));
Date dateTime = new Date(getSunset() * 1000);
return formatter.format(dateTime);
}
public void setSunset(long sunset) {
mSunset = sunset;
}
public double getTemperatureMin() {
return (int)Math.round(mTemperatureMin);
}
public void setTemperatureMin(double temperatureMin) {
mTemperatureMin = temperatureMin;
}
}
package com.joshbgold.WeatherBuddy;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by JoshG on 11/25/2015.
*/
public class DayAdapter extends BaseAdapter {
private Context mContext;
private Day[] mDays;
public DayAdapter(Context context, Day[] days) {
mContext = context;
mDays = days;
}
@Override
public int getCount() {
return mDays.length;
}
@Override
public Object getItem(int position) {
return mDays[position];
}
@Override
public long getItemId(int position) {
return 0; // we aren't going to use this. Tag items for easy reference
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
// brand new
convertView = LayoutInflater.from(mContext).inflate(R.layout.daily_list_item, null);
holder = new ViewHolder();
holder.iconImageView = (ImageView) convertView.findViewById(R.id.iconImageView);
holder.temperatureLabel = (TextView) convertView.findViewById(R.id.temperatureLabel);
holder.dayLabel = (TextView) convertView.findViewById(R.id.dayLabel);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
Day day = mDays[position];
//holder.iconImageView.setImageResource(Day.getIconId());
holder.temperatureLabel.setText(day.getTemperatureMax() + "");
if (position == 0) {
holder.dayLabel.setText("Today");
}
else {
holder.dayLabel.setText(day.getDayOfTheWeek());
}
return convertView;
}
private static class ViewHolder {
ImageView iconImageView; // public by default
TextView temperatureLabel;
TextView dayLabel;
}
}
1 Answer
Seth Kroger
56,416 PointsIn Day.java you have a String mTimeZone and a String mTimezone. Only one of these is being set with the other left as null. You only need to have one of these member variable and should rename the other occurrences of the other to the one you keep.
Josh Gold
12,207 PointsJosh Gold
12,207 PointsHello Seth, that sounds quite reasonable. I will check this solution after Thanksgiving holiday or early next week. Happy Thanksgiving.
Thanks,
Josh