0

I am creating a bitmap which hold Facebook image cropped in a circular shape the code was working fine but wen there is a lot of images in the same activity its giving a null pointer exception while scrolling here is my code

//enter code here
byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
                            Bitmap decodedByte =  BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                     on this line i am getting a null pointer exception
                            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
                            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                            Paint paint = new Paint();
                            paint.setShader(shader);
                            Canvas c = new Canvas(circleBitmap);
                            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
                            holder.civilianImage.setImageBitmap(circleBitmap);

i can see the image presented but while i scroll this error is shown. Any help would be appreciated. And one more thing i getting the image from Facebook but each image is different from the other one in resolution and while cropping the image is losing a lot of it how can i manage this also.

enter code here
public class ListViewAdapter extends ArrayAdapter<NewsFeed>
{
    public static final int missionLayout = 0;
    public static final int badgeLayout = 1;
    private static final int TYPE_MAX_COUNT = badgeLayout + 1;
    private NewsFeed obj;
    private LayoutInflater mInflater;
    public ListViewAdapter ()
    {
        super(CityNewsFeedActivity.this,R.layout.activity_activity_city_news_feed,newsfeed);
        mInflater=(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }
    @Override
    public int getViewTypeCount()

    {
        return TYPE_MAX_COUNT;
    }

    @Override
    public int getItemViewType(int position)
    {
         obj = newsfeed.get(position);
        return obj.category.equals("mission")? missionLayout : badgeLayout;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;

        if (convertView == null)
        {

            holder = new ViewHolder();
   if (obj.accepted_rejected.equals("success"))
                        {
                            holder.relativeLayoutMission.setBackgroundResource(R.drawable.mission_successful);
                            Float alpha = Float.valueOf("0.9");
                            holder.relativeLayoutMission.setAlpha(alpha);
                            byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
                            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
                            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
                            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                            Paint paint = new Paint();
                            paint.setShader(shader);
                            Canvas c = new Canvas(circleBitmap);
                            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
                            holder.civilianImage.setImageBitmap(circleBitmap);
                          }
                      }
                return convertView;
                  }
                 }
enter code here
11-02 20:36:11.344  30864-30864/net.httpiamheroic.herioc E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: net.httpiamheroic.herioc, PID: 30864
java.lang.NullPointerException
        at net.httpiamheroic.herioc.net.httpiamherioc.Activities.CityNewsFeedActivity$ListViewAdapter.getView(CityNewsFeedActivity.java:419)
        at android.widget.AbsListView.obtainView(AbsListView.java:2450)
        at android.widget.ListView.makeAndAddView(ListView.java:1891)
        at android.widget.ListView.fillDown(ListView.java:792)
        at android.widget.ListView.fillGap(ListView.java:756)
        at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5728)
        at android.widget.AbsListView$FlingRunnable.run(AbsListView.java:4826)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:807)
        at android.view.Choreographer.doCallbacks(Choreographer.java:601)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:791)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5867)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
        at dalvik.system.NativeStart.main(Native Method)
Mostafa Addam
  • 6,956
  • 4
  • 20
  • 38

2 Answers2

0

Change your getView method like this Also check what if obj.accepted_rejected.equals("success") is false

 @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        ViewHolder holder=null;
        // Your row view 
        View rowView = convertView;

        if (rowView == null)
        {
            // New holder object 
            holder = new ViewHolder();
            holder.civilianImage = rowView.findViewById(R.id.civilianimageview);// Your xml image view id

                            rowView.setTag(holder);

           } else{
                // Use existing holder object 
              holder=(ViewHolder) rowView.getTag();
           }
        if (obj.accepted_rejected.equals("success")){
            holder.relativeLayoutMission.setBackgroundResource(R.drawable.mission_successful);
            Float alpha = Float.valueOf("0.9");
            holder.relativeLayoutMission.setAlpha(alpha);
            byte[] decodedString = Base64.decode(obj.picture, Base64.DEFAULT);
            Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
            Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
            BitmapShader shader = new BitmapShader(decodedByte, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
            Paint paint = new Paint();
            paint.setShader(shader);
            Canvas c = new Canvas(circleBitmap);
            c.drawCircle(decodedByte.getWidth() / 2, decodedByte.getHeight() / 2, decodedByte.getWidth() / 2, paint);
            holder.civilianImage.setImageBitmap(circleBitmap);
        }else{
            // Handle this case
        }
          return rowView;
     }
Suneel Prakash
  • 389
  • 5
  • 7
0

Unfortunately(Stupidly) multiple methods that has to do with image handling in Android does not throw an exception when provided data that is not an image, they simply return null... WTH?

So as it seems the reason for the error is due to a wrong input to:

byte[] decodedByte = BitmapFactory.decodeByteArray(...)

Which returns null, and forces the app to crash on the next line trying to use the decodedByte

Bitmap circleBitmap = Bitmap.createBitmap(decodedByte.getWidth(), decodedByte.getHeight(), Bitmap.Config.ARGB_8888);
TacB0sS
  • 10,106
  • 12
  • 75
  • 118