1

I am working on a simple app to take a picture and place it in an ImageView, it works if I accept the picture, however if I choose not to use the picture, I get "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.os.Bundle.get(java.lang.String)' on a null object reference". Debugging it I can see mExtra=null, there I implemented a if\else statement but to no avail the app still crash if I reject the picture.

Here is the code snippet:

 public class MainActivity extends AppCompatActivity {
int scoreTeamA = 0;
int scoreTeamB = 0;

Button btnTakePhoto;
ImageView imgTakenPhoto;
private static final int CAM_REQUEST = 1313;
// private static final String EXTRAS_CAMERA_FACING = "android.intent.extras.CAMERA_FACING";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btnTakePhoto = (Button) findViewById(R.id.take_left_picture);
    imgTakenPhoto = (ImageView) findViewById(R.id.left_player_image);//left_player_image imageView name

    btnTakePhoto.setOnClickListener(new btnTakePhotoClicker());
}

public void imgClick(View view) {
    Toast.makeText(this, "imageview clicked", Toast.LENGTH_SHORT).show();
    Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraintent, CAM_REQUEST);
}

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

    if (requestCode == CAM_REQUEST) {

        Bitmap thumbnail = (Bitmap) data.getExtras().get("data");

        if (thumbnail == null) {
            //if picture not accepted to be used set the imageView to the star2.png file
            imgTakenPhoto.setImageResource(R.drawable.star2);
            Toast.makeText(this, "no image to include", Toast.LENGTH_SHORT).show();
        } else {
            //if picture accepted use it to populate the imageView
            imgTakenPhoto.setImageBitmap(thumbnail);

        }
    }
}
    class btnTakePhotoClicker implements Button.OnClickListener {
        @Override
        public void onClick(View v) {
            Intent cameraintent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraintent, CAM_REQUEST);
        }
    }       

My issue seems to be in the onActivityResult method in the if/else statement, but I am not seeing it? I would greatly appreciate your help, Thanks, Olivier

Andy Turner
  • 137,514
  • 11
  • 162
  • 243
olivier
  • 63
  • 6
  • It seems that `data.getExtras()` returns `null`. – Titus Jul 10 '16 at 20:44
  • yes it is correct, data.getExtras() returns null, therefore I thought that if it is null I would set it to a png file named "start2" with the following line: if (thumbnail == null) { //if picture not accepted to be used set the imageView to the star2.png file imgTakenPhoto.setImageResource(R.drawable.star2); However it still crashes, that's what I don't understand? – olivier Jul 12 '16 at 03:05
  • The problems is that `data.getExtras()` returns `null` and you're calling `get("data")` on `null`, you need to check if `data.getExtras()` is `null` before calling `get("data")`, ex: `if(data.getExtras() != null){... = data.getExtras().get("data")}`. – Titus Jul 12 '16 at 06:55
  • Thank you for your help, unfortunately there is something I may not be fully grasping yet. I changed the code to first check is data.getExtras().is null, and if it is null I set the ImageView resource to show a png file named "star2". however if data.getExtras() returns null still crashes? if (thumbnail != null) { imgTakenPhoto.setImageBitmap(thumbnail); } else { imgTakenPhoto.setImageResource(R.drawable.star2) } } – olivier Jul 13 '16 at 11:20
  • Just do this and it should work `if(data.getExtras() != null && data.getExtras().get("data") instanceof Bitmap){imgTakenPhoto.setImageResource((Bitmap)data.getExtras().get("data"));}else{imgTakenPhoto.setImageResource(R.drawable.star2);}` – Titus Jul 13 '16 at 21:54
  • Thank you, I will try it – olivier Jul 14 '16 at 23:44

0 Answers0