Android Application crash - Null Pointer Exception

2.5k views Asked by At

I'm developing a simple app which captures an image from camera and saves it to storage. I cannot figure out what is wrong here...when user takes a picture and accept it, the app crashes.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
    Button photoButton = (Button) this.findViewById(R.id.button1);
    photoButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 

            try {
                File f = createImageFile();
                photoFile = Uri.fromFile(f);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                    photoFile);
            startActivityForResult(cameraIntent, CAMERA_REQUEST); 
        }
    });

    Button saveButton = (Button) this.findViewById(R.id.button3);
    saveButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            Thread thread = new Thread(new Runnable(){
                @Override
                public void run() {
                    try {
                        save2Samba("hello", "kfir.txt");
                        //Your code goes here
                    } 
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            thread.start();
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                imageView.setImageBitmap(photo);
            }  
    }

    private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
        imageFileName,  /* prefix */
        ".jpg",         /* suffix */
        storageDir      /* directory */
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

logcat says

12-29 18:01:20.893: E/AndroidRuntime(8026): java.lang.RuntimeException: Failure    delivering result ResultInfo{who=null, request=1888, result=-1, data=null} to activity {com.example.hofyam/com.example.hofyam.MainActivity}: java.lang.NullPointerException

Please help..I'm stuck. Thanks

2

There are 2 answers

3
S.Thiongane On BEST ANSWER

who=null, request=1888, result=-1, data=null

data is null, meaning that the intent you recieved from the finishing activity is null. So calling : data.getExtras() will cause a NPE.

You have to test first if data is not null, like this :

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {  
        if(data != null) {
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
        }
    }  
}

EDIT: If image is not created, may be it's due to your if test, meaning that data is still null. You have to set data values in your finishing activity :

Intent intent = getIntent().putExtra("data", "your data value");

in order to get them back in onActivityResult method :

data.getExtras().getString("data");
0
Yauraw Gadav On

The problem is here, The resultCode will be RESULT_CANCELED if the activity explicitly returned that, didn't return any result, or crashed during its operation.

You should take care of the fields that can return null i.e. data and resultCode.

@Override 
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {   
  super.onActivityResult(requestCode, resultCode, data); 

  if(resultCode != RESULT_CANCELED)
     { 
      if (requestCode == CAMERA_REQUEST) {  
        if (data != null){
            Bitmap photo = (Bitmap) data.getExtras().get("data"); 
            imageView.setImageBitmap(photo);
     } 
      }
       }
        }