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 Build a Self-Destructing Message Android App Retrieving and Viewing Messages Creating a Custom List Adapter for Messages

Anyone else getting a null pointer exception when loading the ImageView?

I downloaded the source files from the teacher's notes and my code matches the example code line-by-line, but for some reason whenever I run the app on my Nexus 4 (KitKat 4.4.3) it crashes and I get a null pointer exception on this line:

holder.iconImageView.setImageResource(R.drawable.ic_action_picture);

Anyone know what's going on or has experienced the same problem? I created and instantiated the static ViewHolder class as described in the video, maybe my static variable handling is wrong for some reason?

3 Answers

Hi Bill,

I got the Null Pointer exception as well, and after a lot of research I found out that I was missing a line, which apparently doesn't cause any exceptions on Ben's emulator on this video, but some videos later he notices that he forgot it, as his ListView throws a Null Pointer exception when trying to scroll. Anyhow, here is the line that you need to add after assigning holder.iconImageView and holder.nameLabel:

holder.iconImageView = (ImageView) convertView.findViewById(R.id.messageIcon);
holder.nameLabel = (TextView) convertView.findViewById(R.id.senderLabel);
convertView.setTag(holder); // Because we use the tag on the else condition, 
                            // we need to set it here first, so that it can be used when 
                           // recycling our list using the getTag() method.

Hope this helps :innocent:

Ah great, this solves the problem. Thank you so much!