[Solved] setImageBitmap() Throwing nullPointerExceprion [duplicate]


This is happening because you are calling below function

setContentView(R.layout.activity_main);

Views are not being initialized. Replcae your code with below code.

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    try {
        BitmapFactory.Options bitmapOptions = new BitmapFactory.Options();
        bitmapOptions.inSampleSize = 2;
        Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img, bitmapOptions);
        //imageView for referencig imageView of layout file
        ImageView imageView=(ImageView)findViewById(R.id.imageView1);
        imageView.setImageBitmap(imageBitmap);
        //setImageBitmap gives nullPointerException
    }catch (Exception e){
        e.printStackTrace();
    }
    
}

solved setImageBitmap() Throwing nullPointerExceprion [duplicate]