views:

335

answers:

4

I cannot for the life of me figure out why I'm getting a NullPointerException.

When a user clicks on a particular image, a dialog window is supposed to pop-up and display a larger version of said image:

private OnClickListener coverListener = new OnClickListener() 
{
 public void onClick(View v) 
 { 
  showDialog(DIALOG_COVER);
 }
};

DIALOG_COVER is set to = 0.

The associated onCreateDialog looks like this:

protected Dialog onCreateDialog(int id) {
 Dialog dialog;
 switch(id) 
 {
  case DIALOG_COVER:
   dialog = new Dialog(mContext);
   dialog.setContentView(R.layout.cover_dialog);
   dialog.setTitle(book.getTitle());
   ImageView coverLarge = (ImageView)findViewById(R.id.coverLarge);
   coverLarge.setImageBitmap(book.getCover());
      break;
  default:
      dialog = null;
 }
 return dialog;
}

For reference, this is cover_dialog.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/coverDialog"
          android:orientation="horizontal"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:padding="10dp">
<ImageView android:id="@+id/coverLarge"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:scaleType="fitStart"
           /></LinearLayout>

Now, when the image previously described is clicked, the application immediately crashes and throws the following error through LogCat:

06-08 13:29:17.727: ERROR/AndroidRuntime(2220): Uncaught handler: thread main exiting     due to uncaught exception 
06-08 13:29:17.757: ERROR/AndroidRuntime(2220): java.lang.NullPointerException
06-08 13:29:17.757: ERROR/AndroidRuntime(2220):     at org.kylehughes.android.brarian.AndroidBrarian.onCreateDialog(AndroidBrarian.java:259)

The line in question refers to this line inside of onCreateDialog:

coverLarge.setImageBitmap(book.getCover());

Basically, I don't get why coverLarge is null at that point. Any help would be much appreciated.

A: 

This

(ImageView)findViewById(R.id.coverLarge);

returns a nullvalue. You may be passing an invalid/malformed id String to the findViewById method. Have you checked (debugged) what this String looks like before you get the NPE?

Andreas_D
It's just a very long number in line with the rest of the IDs in my app.
Kyle Hughes
A: 

Is it possible that you have a different xml file defining the same ID (coverLarge) for a different kind of View (say a Button)? Also, notice how the id you use to setup the dialog is cover_dialog but in the XML file you have coverDialog

Ricardo Villamil
The coverDialog represents the LinearLayout inside of cover_dialog.xml. And I tried renaming the ImageView to something completely ridiculous (and all of the places it is referenced) and I still get the same error.
Kyle Hughes
A: 

You have a few things going on at that line that some debug output would help. I'd start by System.out.println'ing the values of coverLarge and book immediately before the offending line. I know they shouldn't be null, but it wouldn't hurt to rule those possibilities out.

Josh
Oh yeah, and after you've done that, capture the value of book.getCover() and print it out as well. Again, just to make sure.
Josh
coverLarge is null, and both book and book.getCover return addresses to their respective objects.
Kyle Hughes
+1  A: 

What's about:

/** snip **/
LayoutInflater factory = LayoutInflater.from(mContext);
View dialogView = factory.inflate(R.layout.cover_dialog,null);
ImageView coverLarge = (ImageView)dialogView.findViewById(R.id.coverLarge);
dialog = new Dialog(mContext);
dialog.setContentView(dialogView);
dialog.setTitle(book.getTitle());
coverLarge.setImageBitmap(book.getCover());
/** snip **/

Just written from scratch. Please check the syntax

Francesco
Basically I'm looking for the R.id.coverLarge reference into the dialogView while you are looking for it into the Activity view
Francesco
You sir just saved me many more hours of frustration. Thank you very much.
Kyle Hughes