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 trialJohn Weland
42,478 PointsHourAdapter Error "Attempt to invoke method '...' on a null object reference"
So I've followed this course a few times and this time through thought I would attempt a few changes. Like making the Dailylist an recycler view. I assumed I would run into some issue and where here we go. When I run my app and click the hourly button the app crashes.
Heres is the Monitor output
06-03 10:22:49.052 26737-26737/me.johnweland.tempest E/AndroidRuntime: FATAL EXCEPTION: main
Process: me.johnweland.tempest, PID: 26737
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at me.johnweland.tempest.adapter.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:56)
at me.johnweland.tempest.adapter.HourAdapter.onBindViewHolder(HourAdapter.java:31)
at me.johnweland.tempest.adapter.HourAdapter.onBindViewHolder(HourAdapter.java:13)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3283)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1737)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1581)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1490)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1737)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1581)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1490)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:701)
at android.view.View.layout(View.java:17508)
at android.view.ViewGroup.layout(ViewGroup.java:5555)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2049)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1240)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6298)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5969)
at java.lang.reflect.Method.invoke(Native Method)
06-03 10:22:49.052 26737-26737/me.johnweland.tempest E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:801)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:691)
package me.johnweland.tempest.ui;
import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import java.util.Arrays;
import butterknife.BindView;
import butterknife.ButterKnife;
import me.johnweland.tempest.R;
import me.johnweland.tempest.adapter.HourAdapter;
import me.johnweland.tempest.weather.Hour;
public class HourlyActivity extends AppCompatActivity {
private Hour[] mHours;
@BindView(R.id.recyclerView) RecyclerView mRecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly);
ButterKnife.bind(this);
Intent intent = getIntent();
Parcelable[] parcelables = intent.getParcelableArrayExtra(MainActivity.HOURLY_FORECAST);
mHours = Arrays.copyOf(parcelables, parcelables.length, Hour[].class);
HourAdapter adapter = new HourAdapter(mHours);
mRecyclerView.setAdapter(adapter);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.setHasFixedSize(true);
}
}
package me.johnweland.tempest.weather;
import android.os.Parcel;
import android.os.Parcelable;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Hour implements Parcelable{
private long mTime;
private String mSummary;
private double mTemperature;
private String mIcon;
private String mTimezone;
public Hour() {
}
public long getTime() {
return mTime;
}
public void setTime(long time) {
mTime = time;
}
public String getTimezone() {
return mTimezone;
}
public void setTimezone(String timezone) {
mTimezone = timezone;
}
public String getIcon() {
return mIcon;
}
public int getIconId() {
return Forecast.getIconId(mIcon);
}
public void setIcon(String icon) {
mIcon = icon;
}
public int getTemperature() {
return (int) Math.round(mTemperature);
}
public void setTemperature(double temperature) {
mTemperature = temperature;
}
public String getSummary() {
return mSummary;
}
public void setSummary(String summary) {
mSummary = summary;
}
public String getHour() {
SimpleDateFormat formatter = new SimpleDateFormat("h a");
Date date = new Date(mTime * 1000);
return formatter.format(date);
}
@Override
public int describeContents() {
return 0; // ignore
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeLong(mTime);
dest.writeDouble(mTemperature);
dest.writeString(mSummary);
dest.writeString(mIcon);
dest.writeString(mTimezone);
}
private Hour(Parcel in) {
mTime = in.readLong();
mTemperature = in.readDouble();
mSummary = in.readString();
mIcon = in.readString();
mTimezone = in.readString();
}
public static final Creator<Hour> CREATOR = new Creator<Hour>() {
@Override
public Hour createFromParcel(Parcel source) {
return new Hour(source);
}
@Override
public Hour[] newArray(int size) {
return new Hour[size];
}
};
}
package me.johnweland.tempest.adapter;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import me.johnweland.tempest.R;
import me.johnweland.tempest.weather.Hour;
public class HourAdapter extends RecyclerView.Adapter<HourAdapter.HourViewHolder> {
private Hour[] mHours;
public HourAdapter(Hour[] hours) {
mHours = hours;
}
@Override
public HourViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.hourly_list_item, parent, false);
HourViewHolder viewHolder = new HourViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(HourViewHolder holder, int position) {
holder.bindHour(mHours[position]);
}
@Override
public int getItemCount() {
return mHours.length;
}
public class HourViewHolder extends RecyclerView.ViewHolder {
public TextView mTimeLabel;
public TextView mSummaryLabel;
public TextView mTemperatureLabel;
public ImageView mIconImageView;
public HourViewHolder(View itemView) {
super(itemView);
mTimeLabel = (TextView) itemView.findViewById(R.id.timeLabel);
mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
mTemperatureLabel = (TextView) itemView.findViewById(R.id.temperatureLabel);
mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
}
public void bindHour(Hour hour){
mTimeLabel.setText(hour.getHour());
mSummaryLabel.setText(hour.getSummary());
mTemperatureLabel.setText(hour.getTemperature() + "");
mIconImageView.setImageResource(hour.getIconId());
}
}
}
package me.johnweland.tempest.ui;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import me.johnweland.tempest.R;
import me.johnweland.tempest.ui.AlertDialogFragment;
import me.johnweland.tempest.weather.Current;
import me.johnweland.tempest.weather.Day;
import me.johnweland.tempest.weather.Forecast;
import me.johnweland.tempest.weather.Hour;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends Activity {
public static final String TAG = MainActivity.class.getSimpleName();
public static final String DAILY_FORECAST = "DAILY_FORECAST";
public static final String HOURLY_FORECAST = "HOURLY_FORECAST";
private Forecast mForecast;
@BindView(R.id.timeLabel) TextView mTimeLabel;
@BindView(R.id.temperatureLabel) TextView mTemperatureLabel;
@BindView(R.id.humidityValue) TextView mHumidityValue;
@BindView(R.id.precipValue) TextView mPrecipValue;
@BindView(R.id.summaryLabel) TextView mSummaryLabel;
@BindView(R.id.iconImageView) ImageView mIconImageView;
@BindView(R.id.refreshImageView) ImageView mRefreshImageView;
@BindView(R.id.progressBar) ProgressBar mProgressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
mProgressBar.setVisibility(View.INVISIBLE);
final double latitude = 37.8267;
final double longitude = -122.423;
mRefreshImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getForecast(latitude, longitude);
}
});
getForecast(latitude, longitude);
}
private void getForecast(double latitude, double longitude) {
String apiKey = " APIKEY ";
String forecastURL = "https://api.forecast.io/forecast/" +
apiKey +
"/" +
latitude +
"," +
longitude;
if(isNetworkAvailable()) {
toggleRefresh();
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(forecastURL)
.build();
Call call = client.newCall(request);
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
alertUserAboutError();
}
@Override
public void onResponse(Call call, Response response) throws IOException {
runOnUiThread(new Runnable() {
@Override
public void run() {
toggleRefresh();
}
});
try {
String jsonData = response.body().string();
if (response.isSuccessful()) {
mForecast = parseForecastDetails(jsonData);
runOnUiThread(new Runnable() {
@Override
public void run() {
updateDisplay();
}
});
} else {
alertUserAboutError();
}
}
catch (IOException e) {
Log.e(TAG, "Exception caught: ",e);
}
catch (JSONException e) {
Log.e(TAG, "Exception caught: ",e);
}
}
});
} else {
Toast.makeText(this, R.string.network_unavailable_message, Toast.LENGTH_LONG).show();
}
}
private void toggleRefresh() {
if (mProgressBar.getVisibility() == View.INVISIBLE) {
mProgressBar.setVisibility(View.VISIBLE);
mRefreshImageView.setVisibility(View.INVISIBLE);
} else {
mProgressBar.setVisibility(View.INVISIBLE);
mRefreshImageView.setVisibility(View.VISIBLE);
}
}
private void updateDisplay() {
Current current = mForecast.getCurrently();
mTemperatureLabel.setText(current.getTemperature() + "");
mTimeLabel.setText("At " + current.getFormattedTime() + " it will be");
mHumidityValue.setText(current.getHumidity() + "");
mPrecipValue.setText(current.getPrecipChance() + "%");
mSummaryLabel.setText((current.getSummary()));
Drawable drawable = getResources().getDrawable(current.getIconId());
mIconImageView.setImageDrawable(drawable);
}
private Forecast parseForecastDetails(String jsonData) throws JSONException {
Forecast forecast = new Forecast();
forecast.setCurrently(getCurrentDetails(jsonData));
forecast.setHourly(getHourlyDetails(jsonData));
forecast.setDaily(getDailyDetails(jsonData));
return forecast;
}
private Day[] getDailyDetails(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.setSummary(jsonDay.getString("summary"));
day.setTempMax(jsonDay.getDouble("temperatureMax"));
day.setTempMin(jsonDay.getDouble("temperatureMin"));
day.setIcon(jsonDay.getString("icon"));
day.setTime(jsonDay.getLong("time"));
day.setTimezone(timezone);
days[i] = day;
}
return days;
}
private Hour[] getHourlyDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject hourly = forecast.getJSONObject("hourly");
JSONArray data = hourly.getJSONArray("data");
Hour[] hours = new Hour[data.length()];
for ( int i = 0; i < data.length(); i++ ) {
JSONObject jsonHour = data.getJSONObject(i);
Hour hour = new Hour();
hour.setSummary(jsonHour.getString("summary"));
hour.setTemperature(jsonHour.getDouble("temperature"));
hour.setIcon(jsonHour.getString("icon"));
hour.setTime(jsonHour.getLong("time"));
hour.setTimezone(timezone);
hours[i] = hour;
}
return hours;
}
private Current getCurrentDetails(String jsonData) throws JSONException {
JSONObject forecast = new JSONObject(jsonData);
String timezone = forecast.getString("timezone");
JSONObject currently = forecast.getJSONObject("currently");
Current current = new Current();
current.setHumidity(currently.getDouble("humidity"));
current.setTime(currently.getLong("time"));
current.setIcon(currently.getString("icon"));
current.setPrecipChance(currently.getDouble("precipProbability"));
current.setSummary(currently.getString("summary"));
current.setTemperature(currently.getDouble("temperature"));
current.setTimezone(timezone);
return current;
}
private boolean isNetworkAvailable() {
ConnectivityManager manager = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkinfo = manager.getActiveNetworkInfo();
boolean isAvalable = false;
if(networkinfo != null && networkinfo.isConnected()) {
isAvalable = true;
}
return isAvalable;
}
private void alertUserAboutError() {
AlertDialogFragment dialog = new AlertDialogFragment();
dialog.show(getFragmentManager(), "error_dialog");
}
@OnClick (R.id.dailyButton)
public void startDailyActivity (View view) {
Intent intent = new Intent(this, DailyActivity.class);
intent.putExtra(DAILY_FORECAST, mForecast.getDaily());
startActivity(intent);
}
@OnClick (R.id.hourlyButton)
public void startHourlyActivity (View view) {
Intent intent = new Intent(this, HourlyActivity.class);
intent.putExtra(HOURLY_FORECAST, mForecast.getHourly());
startActivity(intent);
}
}
4 Answers
Steve Hunter
57,712 PointsTypo! - android:id=
"@+id/temperaturLabel
"
Change that to temperatureLabel
Let me know if that works.
Steve.
Steve Hunter
57,712 PointsThat's in your hourly_list_item
layout within the relevant TextView
.
Steve.
John Weland
42,478 Pointsyeah I found that too, Thanks! never enough coffee I swear. Now I've got to work through this error with the day portion this is where I made a recycler view different than the course work.
FAIZEL KHAN
2,328 PointsI have same same problem but i am not able to figure out.Can you help me?
Steve Hunter
57,712 PointsYes, we can hep - can you be a little more precise about what your problem is? Thanks.
Steve.
Kourosh Raeen
23,733 PointsHi John - I just compared your code with the code from the "Android Lists and Adapters" course and it looks almost identical to that, except for the version of the ButterKnife librray and a few name changes. The error message points to line 56 in HourAdapter class and apparently one of the widget references, mTemperatureLabel
, is null so calling setText()
on that is causing the error, though I'm not sure why it is null. Have you tried putting a breakpoint in the HourViewHolder constructor to see if the member variables are being set? Also, can you post the relevant layout file?
John Weland
42,478 Pointslooks like its my naming of temperaturLabel not temperatureLabel... woomp woomp
Steve Hunter
57,712 PointsHi John,
This is being caused by the setText
method failing as it is being called on a null object. So, mTemperatureLabel
is not pointing to anything. I've not got my code to hand and can't recall where mTemperatureLabel
is bound to the R.id etc. But that's what the error is saying; the attempt to use setText
is failing because you're trying to set the text of a null. Therefore, mTemperatureLabel
is pulling back as null.
I hope that helps a little - I'll be at my Mac later so will have my code available.
Steve.
Kourosh Raeen
23,733 PointsHello again John - In your HourlyActivity class you have the following line:
@BindView(R.id.recyclerView) RecyclerView mRecyclerView;
However, in your activity_hourly.xml file you are using a different id for the RecyclerView:
android:id="@+id/hourlyRecyclerView"
John Weland
42,478 Pointsyeah I had just edited those because I have a different error in my daily stuff and thought maybe it had to do with two recycler views with the same name.... it turns out it doesn't so I'm going ot have to work through that.
John Weland
42,478 PointsJohn Weland
42,478 PointsAs requested