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

Android Android Lists and Adapters (2015) Lists with RecyclerViews Using a Layout Manager

James N
James N
17,864 Points

null pointer exeption

I am getting a nullPointerException in HourAdapter.java:

HourAdapter.java
package james.stormy.Adapters;

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 james.stormy.R;
import james.stormy.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 i) {
        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 mTempLabel;
        public ImageView mIconImageView;

        public HourViewHolder(View itemView) {
            super(itemView);

            mTimeLabel = (TextView) itemView.findViewById(R.id.time_label);
            mSummaryLabel = (TextView) itemView.findViewById(R.id.summaryLabel);
            mTempLabel = (TextView) itemView.findViewById(R.id.tempLabel);
            mIconImageView = (ImageView) itemView.findViewById(R.id.iconImageView);
        }

        public void bindHour(Hour hour) {
            mTimeLabel.setText(hour.getHour());
            mSummaryLabel.setText(hour.getSummary());
            mTempLabel.setText(hour.getTemperature() + "");
            mIconImageView.setImageResource(hour.getIconId());
        }
    }
}

LOGCAT:

logcat.java
04-27 16:42:43.132    1340-1340/james.stormy E/AndroidRuntime FATAL EXCEPTION: main
    Process: james.stormy, PID: 1340
    java.lang.NullPointerException
            at james.stormy.Adapters.HourAdapter$HourViewHolder.bindHour(HourAdapter.java:56)
            at james.stormy.Adapters.HourAdapter.onBindViewHolder(HourAdapter.java:31)
            at james.stormy.Adapters.HourAdapter.onBindViewHolder(HourAdapter.java:13)
            at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:4138)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3448)
            at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
            at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
            at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
            at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
            at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
            at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
            at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
            at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
            at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
            at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
            at android.view.View.layout(View.java:14817)
            at android.view.ViewGroup.layout(ViewGroup.java:4631)
            at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
            at android.view.Choreographer.doCallbacks(Choreographer.java:574)
            at android.view.Choreographer.doFrame(Choreographer.java:544)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

Hello,

I'm not seeing anything in your HourAdapter offhand that appears to be causing the problem. Could you also post your HourlyForcastActivity.java file as well as your MainActivity.java file?

James N
James N
17,864 Points

sure!

HourlyForecastActivity.java
package james.stormy.UI;

import android.content.Intent;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import james.stormy.Adapters.HourAdapter;
import james.stormy.R;
import james.stormy.weather.Hour;


public class HourlyForecastActivity extends ActionBarActivity {
    private Hour[] mHours;

    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_hourly_forecast);
        ButterKnife.inject(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);
    }
}

Hello,

I'm still not seeing anything offhand that would be causing null values to be present. Could you post your MainActivity.java file and your Hour.java file? Another thing you could do is use the debugger and at certain points, verify the contents of mHours to make sure it has data.

2 Answers

David Burnham
David Burnham
7,023 Points

This question seems to be on multiple threads. I had the same issue and found that the error was not with the value that it was reading, but the place it was posting. Check that your label on your hourly_list_item matches the name in HourAdapter. You may be referencing a different temp label from a different view.

James N
James N
17,864 Points

I don't understand what you mean can you please explain this problem more in-depth?

David Burnham
David Burnham
7,023 Points

This project creates several labels that have similar names. If you are not careful, you will set your data to the wrong label. What you want to check is that the label id on the XML for hourly_list_item matches the name in your hourly adaptor.

In your code above, this is the line in your HourAdapter:

mTempLabel = (TextView) itemView.findViewById(R.id.tempLabel);

Check to make sure that the tempLabel on the hourly_list_item has the same name. It is possible that R.id.tempLabel is pointing to a temperature on a different activity since you have temp used in several activities.

I'm going to second the verifying your ids match between the java file and the layout file. I don't know if I just autocorrected it the last time I looked, but this time I'm double checking your code against the code provided in the teacher's notes, I see that you have the ids not maching the defaults. For example, in the hourly_list_item.xml file provided in the code from the teacher's notes, it has the label for time as timeLabel while your code has it as time_label. Similarily, you have the temperature label with an id of tempLabel in your java file, while the provided xml in the teacher's notes have it as temperatureLabel. Again, I'm sorry I didn't catch this the first time I tried inserting your code into the base files from the teacher's notes.

I know a few days ago people were having trouble with the API not giving the full data in the JSON files and that was causing some people to get errors. Is your error still happening? If so, do you think you could post it to Github so that we can take a deeper look at what its doing?

James N
James N
17,864 Points

I don't have github... nor do I want it right now, so how else can I show you what's going on?

If you're still having trouble, there's a chance that the issue is either in your MainActivity.java or your Hours.java file. Could you post those?

James N
James N
17,864 Points

sure!

hour.java
package james.stormy.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 getSummary() {
        return mSummary;
    }

    public void setSummary(String summary) {
        mSummary = summary;
    }

    public int getTemperature() {
        return (int) Math.round(mTemperature);
    }

    public void setTemperature(double temperature) {
        mTemperature = temperature;
    }

    public String getIcon() {
        return mIcon;
    }

    public int getIconId() {
        return Forecast.getIconId(mIcon);

    }

    public void setIcon(String icon) {
        mIcon = icon;
    }

    public String getTimezone() {
        return mTimezone;
    }

    public void setTimezone(String timezone) {
        mTimezone = timezone;
    }

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

    @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];
        }
    };
}
MainActivity.java
package james.stormy.UI;

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.support.v7.app.ActionBarActivity;
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 com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import james.stormy.R;
import james.stormy.weather.Current;
import james.stormy.weather.Day;
import james.stormy.weather.Hour;
import james.stormy.weather.Forecast;


public class MainActivity extends ActionBarActivity {

    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;
    @InjectView(R.id.time_label) TextView mTimeLabel;
    @InjectView(R.id.TempLabel) TextView mTemperatureLabel;
    @InjectView(R.id.hum_value) TextView mHumidityValue;
    @InjectView(R.id.PRECIP_VALUE) TextView mPrecipValue;
    @InjectView(R.id.summaryLabel) TextView mSummaryLabel;
    @InjectView(R.id.icon_image_view) ImageView mIconImageView;
    @InjectView(R.id.progressBar) ProgressBar mProgressBar;
    @InjectView(R.id.location_view) TextView mLocationLabel;
    @InjectView(R.id.refreshImageView) ImageView mRefreshButton;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.inject(this);

        mProgressBar.setVisibility(View.INVISIBLE);



        final double latitude = 42.683333;
        final double longitude = -81.797222;

        mRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getForecast(latitude, longitude);
            }
        });

        getForecast(latitude,longitude);
        Log.d(TAG,"Main UI is running!");

    }



    private void getForecast(double lat, double Long) {
        mSummaryLabel.setText("Getting Current Weather...");
        String apiKey = "b964c75d003c3f8b1a737880cb6deabb";


        String forecastUrl = "https://api.forecast.io/forecast/"+apiKey+"/"+lat+","+Long;
        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(Request request, IOException e)  {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                            AlertUserAboutError();

                        }
                    });


                }

                @Override
                public void onResponse(Response response) throws IOException {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            toggleRefresh();
                        }
                    });
                    try {
                        String jsonData = response.body().string();
                        Log.v(TAG, jsonData);

                        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 {

            mSummaryLabel.setText(getString(R.string.error_message_simplified));
            Toast.makeText(this, getString(R.string.network_unavailable_message), Toast.LENGTH_LONG).show();
        }
    }

    private void toggleRefresh() {
        if (mProgressBar.getVisibility()==View.VISIBLE) {
        mProgressBar.setVisibility(View.INVISIBLE);
        mRefreshButton.setVisibility(View.VISIBLE);
        }
        else {
            mProgressBar.setVisibility(View.VISIBLE);
            mRefreshButton.setVisibility(View.INVISIBLE);
        }
    }

    private void updateDisplay() {
        Current current = mForecast.getCurrent();
        mTemperatureLabel.setText(current.getTemp() + "");


        mTimeLabel.setText("At "+ current.getFormattedTime()+ " it will be");
        mHumidityValue.setText(current.getHum() + "");
        mPrecipValue.setText(current.getPrecipChance()+"%");
        mSummaryLabel.setText(current.getSummary());
        Drawable drawable = getResources().getDrawable(current.getIconId());
        mIconImageView.setImageDrawable(drawable);
        mLocationLabel.setText(current.getTimeZone());

    }

    private Forecast parseForecastDetails(String jsonData) throws JSONException{
        Forecast Forecast = new Forecast();

            Forecast.setCurrent(getCurrentDetails(jsonData));
            Forecast.setHourlyForecast(getHourlyForecast(jsonData));
            Forecast.setDailyForcast(getDailyForecast(jsonData));

        return Forecast;
    }

    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.setSummary(jsonDay.getString("summary"));
            day.setIcon(jsonDay.getString("icon"));
            day.setTempMax(jsonDay.getDouble("temperatureMax"));
            day.setTime(jsonDay.getLong("time"));
            day.setTimeZone(timeZone);

            days[i] = day;
        }

        return days;
    }

    private Hour[] getHourlyForecast(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 boolean isNetworkAvailable() {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = manager.getActiveNetworkInfo();
        boolean isAvailable = false;
        if (networkInfo != null && networkInfo.isConnected()) {
            isAvailable = true;

        }
        return isAvailable;
     }

    private void AlertUserAboutError() {
        mSummaryLabel.setText(getString(R.string.error_message_simplified));
        AlertDialogFragment dialog = new AlertDialogFragment();
        dialog.show(getFragmentManager(),"error_dialog");
    }
    private Current getCurrentDetails(String data) throws JSONException{
        JSONObject forecast = new JSONObject(data);
        String timeZone = forecast.getString("timezone");
        Log.i(TAG,"From JSON:" + timeZone);

        JSONObject currently = forecast.getJSONObject("currently");

        Current current = new Current();
        current.setHum(currently.getDouble("humidity"));
        current.setTime(currently.getLong("time"));
        current.setIcon(currently.getString("icon"));
        current.setPrecipChance(currently.getDouble("precipProbability"));
        current.setSummary(currently.getString("summary"));
        current.setTemp(currently.getDouble("temperature"));
        current.setTimeZone(timeZone);

        Log.d(TAG, current.getFormattedTime());

        return current;
    }

    @OnClick (R.id.dailyButton)
    public void startDailyActivity(View view) {
        Intent intent = new Intent(this, DailyForecastActivity.class);
        intent.putExtra(DAILY_FORECAST, mForecast.getDailyForcast());
        startActivity(intent);
    }
    @OnClick (R.id.hourlyButton)
    public  void  startHourlyActivity(View view) {
        Intent intent = new Intent(this, HourlyForecastActivity.class);
        intent.putExtra(HOURLY_FORECAST, mForecast.getHourlyForecast());
        startActivity(intent);
    }


}