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 trialGuy Bridge
Android Development Techdegree Graduate 24,991 PointsGetting 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)
Seth Kroger
56,413 PointsCode?
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
61,321 PointsIn 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
56,413 PointsIn 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
Android Development Techdegree Graduate 24,991 PointsThanks 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
Courses Plus Student 6,497 PointsPlease link your code. I wish everyone wouldnt just post errors & expect us to figure it out. Its much efficient if we see the code.
Guy Bridge
Android Development Techdegree Graduate 24,991 PointsGuy Bridge
Android Development Techdegree Graduate 24,991 PointsLine 59 looks like:
public void bindView(int position) { mIndex = position; mTextView.setText(Recipes.names[position]); mImageView.setImageResource(Recipes.resourceIds[position]); }