Thursday, September 5, 2013

imageView.setImageURI Null Pointer Exception (NPE)

I was trying to change the source of an "imageView" in my design , but there was a pain in the ass that stopped me progressing.  I was getting the error as shown below.




Check the line including  "Caused by" statement.   There was no error at all, I was totally sure of it. However there was something that I was missing -->    the order of the important lines of codes.
If you check the onCreate() method of my application, you may see that it seems pretty well :) (look at that carefully !!! lol )  there is a huge mistake I made though.    The problem is that ,  setContentView(xyz)  is a pretty important line of code stating that  "I will Set the activity content to an explicit view. "   .    But somehow ( I know why, because it was in the middle of the night me was trying to build up something with my sleepless eyes ) I was successful enough to put that marked line of code between  two important lines as shown below.  It troubled me as expected.  It took almost an hour for me to focus on my problem and fix it (for god's sake, I was so sleepless) .  Please just take that line to any place with making sure that it comes right after setContentView() method.  To me, programmers should not code as drivers should not drive when they are sleepless.

 public void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       this.imageView = (ImageView)this.findViewById(R.id.imageView1);
       setContentView(R.layout.takephoto);
     
       //Call capture image method
       /*Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
           
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
     */
.
.
.
.

}


Regards,

SS

No comments:

Post a Comment