8

I'm trying to get image from sd card into bitmap for displaying in Image-view.After run the application first two to three images are displayed but when i scrolling list application is crashed and getting exception of NullPointerException : java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference at com.example.tazeen.classnkk.Activity1$MyListAdapter.getView(Activity1.java:357)

                if(objElement.endsWith(mp3_Pattern))
                {
                    Log.e("Mp3 ", " ends with ");

                    {
                        int  scalFactor  = fixHeight / h ;
                        newWidth = (int)( w * scalFactor);
                        Log.e("scalFactor "," = " + scalFactor);
                        Log.e("newWidth "," = " + newWidth);
                        Log.e("resizing...  ","in if condition");
                        new_Height = fixHeight ;
                    }
                    else
                    {
                        newWidth = w;
                        new_Height = h;
                    }


                    Uri uri = Uri.fromFile(new File(objElement));
                    Picasso.with(getContext()).load(uri).resize(newWidth, new_Height).placeholder(R.drawable.img_placeholder).into(imageView , new com.squareup.picasso.Callback() {
                        @Override
                        public void onSuccess() {
                            if (pBar != null) {
                                pBar.setVisibility(View.GONE);
                            }
                        }
                        @Override
                        public void onError() {

                        }
                    });
                }

                holder.linearLayout.addView(imageView);
                holder.linearLayout.addView(pBar);

Here is log information

08-28 14:22:14.077    2947-2947/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.tazeen.classnkk, PID: 2947
    java.lang.NullPointerException: Attempt to invoke virtual method 'int android.graphics.Bitmap.getWidth()' on a null object reference
            at com.example.tazeen.classnkk.AllPosts_Page$MyListAdapter.getView(AllPosts_Page.java:357)
            at android.widget.AbsListView.obtainView(AbsListView.java:2347)
            at android.widget.ListView.makeAndAddView(ListView.java:1864)
            at android.widget.ListView.fillDown(ListView.java:698)
            at android.widget.ListView.fillGap(ListView.java:662)
            at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4991)
            at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3418)
            at android.widget.AbsListView.onTouchMove(AbsListView.java:3801)
            at android.widget.AbsListView.onTouchEvent(AbsListView.java:3632)
            at android.view.View.dispatchTouchEvent(View.java:8471)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2399)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2092)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2106)
androidTag
  • 5,053
  • 7
  • 19
  • 28

4 Answers4

3

try this. inside your if (objElement.endsWith(png_Pattern))

  BitmapFactory.Options options = new BitmapFactory.Options();
  options.inPreferredConfig = Bitmap.Config.ARGB_8888;
  Bitmap bitmap = BitmapFactory.decodeFile(objElement, options);
harshitpthk
  • 4,058
  • 2
  • 24
  • 32
  • But in my String objElement﹕ is already getting sd card path like objElement﹕ = /mnt/sdcard/SDImages/95_20150824112324025.png. – androidTag Aug 28 '15 at 09:56
  • i have updated the answer also refer http://stackoverflow.com/a/8710690/1552136. in case doesn't solve this let me know i will try this. – harshitpthk Aug 28 '15 at 10:09
  • Still getting Bitmap is null , what is wrong in code. – androidTag Aug 28 '15 at 10:13
  • where are you testing this on your device or emulator? – harshitpthk Aug 28 '15 at 10:16
  • testing on emulator and testing device also. – androidTag Aug 28 '15 at 10:20
  • Why bitmap is null ? Can someone help ? Please . – androidTag Aug 28 '15 at 11:53
  • I have just check the if condition in if statement BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap bitmap = BitmapFactory.decodeFile(objElement, options); if(bitmap != null ) { here displaying the HorizontalScrollView with images } – androidTag Aug 29 '15 at 03:44
1

Try replacing Bitmap bitmapSize = BitmapFactory.decodeFile(objElement); with

  File file = new File(objElement);
  if (file.exists()) {
     Bitmap bitmapSize = BitmapFactory.decodeFile(file.getAbsolutePath());
  }
Prasad
  • 3,462
  • 1
  • 23
  • 28
0

It seems that you have images which could not be decoded.

From documentation:

/**
 * Decode a file path into a bitmap. If the specified file name is null,
 * or cannot be decoded into a bitmap, the function returns null.
 *
 * @param pathName complete path name for the file to be decoded.
 * @return the resulting decoded bitmap, or null if it could not be decoded.
 */
public static Bitmap decodeFile(String pathName) {
    return decodeFile(pathName, null);
}
Oleksandr
  • 6,226
  • 1
  • 46
  • 54
  • so this snippet should help decode it when you get a null pointer exception on bitmap.getCongif()? – user3919920 Jan 30 '20 at 21:07
  • @user3919920 no, it will not help. This snippet shows the fragment of Android Source code which explains why null can be returned. – Oleksandr Feb 03 '20 at 14:07
0

I know i am little late for this answer. however today i was facing the same issue. Setting the image using bitmap in the picasso, didn't fixed my problem. I tried the below one.

Picasso.with(holder.mRootView.getContext()).load(new File(trackingNumbers.get(position).getImageUrl())).error(R.drawable.no_image_place_holder).noFade().placeholder(R.drawable.no_image_place_holder).into(circularImageView);

Thank You

Deep Rathod
  • 69
  • 3
  • 14