"Error Handling in Swift" was retired on May 31, 2020.

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 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,413 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,413 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,497 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.