Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Android Fragments Tablet Time Adapt Together

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Getting NPE now that I have created the RecyclerAdapter class

Hi,

I have just completed the steps in this video where we create the RecyclerAdapter class. The exception is:

FATAL EXCEPTION: main Process: au.com.wsit.smellslikebakin, PID: 6420 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at au.com.wsit.smellslikebakin.RecyclerAdapter$ListViewHolder.bindView(RecyclerAdapter.java:59) at au.com.wsit.smellslikebakin.RecyclerAdapter.onBindViewHolder(RecyclerAdapter.java:31) at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5453)

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Line 59 looks like:

public void bindView(int position) { mIndex = position; mTextView.setText(Recipes.names[position]); mImageView.setImageResource(Recipes.resourceIds[position]); }

Seth Kroger
Seth Kroger
56,403 Points

Code?

It looks like mImageView wasn't set correctly, though mTextView apparently was. You would have gotten a null pointer error on that line first if it wasn't.

Oziel Perez
Oziel Perez
61,321 Points

In my case, I couldn't even get an error! App just closed at start up and there was no stacktrace in the logcat. If you are experiencing this, check if the answer below will help you.

3 Answers

Seth Kroger
Seth Kroger
56,403 Points

In the two layout files that you choose from, grid_item.xml and list_item.xml, the ImageViews have different id names. Only one id is used in RecyclerAdapter. You need to have the same id name in both layouts for it to work.

Guy Bridge
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Guy Bridge
Android Development Techdegree Graduate 24,991 Points

Thanks Seth, Yes that's working now.

I also had to surround the isTablet = getResources().getBoolean(R.bool.is_tablet); in a try catch block.

It seems that when the device is not a tablet this causes an exception;

 try
        {
            isTablet = getResources().getBoolean(R.bool.is_tablet);
        }
        catch(Exception e)
        {
            isTablet = false;
        }
Cindy Lea
PLUS
Cindy Lea
Courses Plus Student 6,485 Points

Please link your code. I wish everyone wouldnt just post errors & expect us to figure it out. Its much efficient if we see the code.