-1

I wanted to load an image into an imageview of the app from my mobile storage. Just to be sure, I kept the image in both internal and external storage. But while compiling, I am getting a null pointer exception. I also placed the same image in drawable folder and tried to set the imageview from there. But faced the same issue. Can anyone help me here!. I am adding the code snippets that I tried.

Method-1:

File imageFile = new File(Environment.getExternalStorageDirectory(),"golden_eagle.jpg");
if(imageFile.exists()) {
    Log.v(LOGTAG, "Image file exists");
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap outputImageBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), options);
    ImageView imageView = (ImageView)findViewById(R.id.sample_image);
    imageView.setImageBitmap(outputImageBitmap);
}

Method-2:

Bitmap bitimage;
bitimage = BitmapFactory.decodeResource(getResources(), R.drawable.golden_eagle);
if(bitimage == null)
Log.v(LOGTAG,"bitimage is empty");
else
Log.v(LOGTAG,"bitimage is not empty");
ImageView imageView = (ImageView)findViewById(R.id.sample_image);
imageView.setImageBitmap(bitimage);

Method-3:

ImageView imageView = (ImageView)findViewById(R.id.sample_image);
int resid = getResources().getIdentifier("golden_eagle","drawable",getPackageName());
imageView.setImageResource(resid);
//imageView.setImageResource(R.drawable.golden_eagle);

Method 4:

//Some class extending AppCompatActivity
asyncTask.execute();

//some asyncTask

onpostExecute(){
    //Method:1
    //Method:2
    //Method:3
}

All the above methods ended with same compilation error

12-24 20:16:42.520 1349-1349/? E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.home.BequestProto, PID: 1349
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.home.BequestProto/com.example.home.BequestProto.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at com.example.home.BequestProto.MainActivity.onCreate(MainActivity.java:75)
    at android.app.Activity.performCreate(Activity.java:6582)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1113)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2532)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2667) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1494) 
    at android.os.Handler.dispatchMessage(Handler.java:111) 
    at android.os.Looper.loop(Looper.java:207) 
    at android.app.ActivityThread.main(ActivityThread.java:5776) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 

The Error is occurring at at the line where I am setting image to imageview. I know that It is a null pointer exception because some where I forgot to initialize the required object. I want to know which one.

3 Answers3

1

"Method 4:" Shows that you are executing with async task. I guess the layout changed and it tryed to set the image after.

    ImageView imageView = (ImageView)findViewById(R.id.sample_image);
    if (imageView != null)
            imageView.setImageBitmap(outputImageBitmap);

This will set the image only if View is present

0
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference

This would appear to be from "Method-3", where you are calling setImageResource().

This error means that this line is where your problem lies:

ImageView imageView = (ImageView)findViewById(R.id.sample_image);

imageView is null in all four cases, because findViewById() cannot find this widget.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Double check if you have ImageView with id sample_image. It's not found in your layout

piotrpo
  • 12,398
  • 7
  • 42
  • 58