NicklasN NicklasN - 1 year ago 88
Android Question

Null pointer on a imageview, don't know how to pass it to a view

I have a app that takes a picture and then I want to see that picture when its taken. And that works fine, I get a picture and save it to storage. So I made a view for it called "activity_image.xml. But I get this nullpointer:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(' on a null object reference

Here I try to pass it to imageview from my main activity, and I cant figure out how to do this properly:

static final int REQUEST_TAKE_PHOTO = 1;
static final int REQUEST_IMAGE_CAPTURE = 1;
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;
protected void onCreate(Bundle savedInstanceState) {


mVisible = true;
mControlsView = findViewById(;
mContentView = findViewById(;
sButton = findViewById(;
//final Intent cam = new Intent("");

// Set up the user interaction to manually show or hide the system UI.
mContentView.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

sButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view){

// Upon interacting with UI controls, delay any scheduled hide()
// operations to prevent the jarring behavior of controls going away
// while interacting with the UI.

private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
System.out.println("ERR CANNOT CREATE FILE");
// Continue only if the File was successfully created
if (photoFile != null) {
Uri photoURI = FileProvider.getUriForFile(this,
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
startActivityForResult(takePictureIntent, REQUEST_TAKE_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 = getExternalFilesDir(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;
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
try {
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));

mImageView = (ImageView)findViewById(;

} catch (IOException e) {


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
android:orientation="vertical" android:layout_width="match_parent"

android:layout_weight="0.95" />

Answer Source
mImageView = (ImageView)findViewById(;
mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));

Just Reformate your code like this
change this :

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download