0

In my adapter i have used byte array for image in that i am getting a exception stating Attempt to get length of null array i don't why it now shows me a error right now before it works fine

Adapter:

public class ImageListAdapter extends BaseAdapter{
Context context;
ArrayList<ImagelistItems> imageList;

public ImageListAdapter(Context context, ArrayList<ImagelistItems> list) {

    this.context = context;
    imageList = list;
}



@Override
public int getCount() {

    return imageList.size();
}

@Override
public Object getItem(int position) {

    return imageList.get(position);
}

@Override
public long getItemId(int position) {

    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup arg2) {
    ImagelistItems imagelistItems = imageList.get(position);

    if (convertView == null) {
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.imagelist_items, null);

    }
    byte[] outImage=imagelistItems.getImage();
    ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);
    final Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    ImageView img=(ImageView) convertView.findViewById(R.id.imgview);
    img.setImageBitmap(theImage);
    final TextView merchname = (TextView) convertView.findViewById(R.id.lsmerchantname);
    merchname.setText(imagelistItems.getMerchantname());
    final TextView paid = (TextView) convertView.findViewById(R.id.lspaiddate);
    paid.setText(imagelistItems.getPaidon());
    TextView status = (TextView) convertView.findViewById(R.id.lsstatus);
    status.setText(imagelistItems.getStatus());
    final TextView amt = (TextView) convertView.findViewById(R.id.lsamount);
    amt.setText(imagelistItems.getAmount());
    ImageButton oofbutton=(ImageButton)convertView.findViewById(R.id.btnofflinebutton);
    oofbutton.setVisibility(View.INVISIBLE);
    final TextView category=(TextView) convertView.findViewById(R.id.lscategory);
    category.setText(imagelistItems.getCategory());
    final TextView paymode=(TextView) convertView.findViewById(R.id.lspaidwith);
    paymode.setText(imagelistItems.getPaymmode());
    final TextView comment=(TextView) convertView.findViewById(R.id.lscomment);
    comment.setText(imagelistItems.getComment());
    final TextView moneydet=(TextView) convertView.findViewById(R.id.moneydetails);
    final String moen;
    CurrenctSession currenctSession=new CurrenctSession(context);
    if(currenctSession.isLoggedIn()){
        HashMap<String, String> cur = currenctSession.getUserDetails();
        moen=cur.get(currenctSession.KEY_CURRENCY);
        moneydet.setText(moen);
    }else{
        moneydet.setVisibility(View.INVISIBLE);
    }
    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String am=amt.getText().toString();
            if(am.contains("km")|am.contains("m")){
                Intent i=new Intent(context,MapsDetailPage.class);
                i.putExtra("distance",amt.getText().toString());
                i.putExtra("origin",merchname.getText().toString());
                i.putExtra("dest",paymode.getText().toString());
                i.putExtra("catg",category.getText().toString());
                i.putExtra("pdate",paid.getText().toString());
                i.putExtra("comm",comment.getText().toString());
                context.startActivity(i);
            }else{
                Intent i=new Intent(context,DetailsPage.class);
                i.putExtra("bitmap",theImage);
                i.putExtra("amount",amt.getText().toString());
                i.putExtra("mername",merchname.getText().toString());
                i.putExtra("paidon",paid.getText().toString());
                i.putExtra("catg",category.getText().toString());
                i.putExtra("paym",paymode.getText().toString());
                i.putExtra("comm",comment.getText().toString());
                context.startActivity(i);
            }

        }
    });
    return convertView;

}

}

ImageList:

private void offlinemodes() {
    db=new DatabaseHandler(getApplicationContext());
    list = new ArrayList<ImagelistItems>();
    list=db.getAllLabels();

    if(loginSession.isLoggedIn()){
        loginSession.checkLogin();
          if(imageListAdapter==null){
              listView.setEmptyView(findViewById(R.id.empty));
           }else{

        imageListAdapter=new ImageListAdapter(ListMode.this,list);
        listView.setAdapter(imageListAdapter);
        Toast.makeText(getApplicationContext(), db.getSyncStatus(), Toast.LENGTH_LONG).show();
         }
    }else{
        Intent i = new Intent(ListMode.this, LoginPAge.class);
        startActivity(i);
        ListMode.this.finish();
    }
}

Error:

6-17 12:49:14.473  15372-15372/nidhinkumar.reccs E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nidhinkumar.reccs, PID: 15372
java.lang.NullPointerException: Attempt to get length of null array
        at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:60)
        at nidhinkumar.reccs.ImageListAdapter.getView(ImageListAdapter.java:63)
        at android.widget.AbsListView.obtainView(AbsListView.java:2467)
        at android.widget.ListView.makeAndAddView(ListView.java:1894)
        at android.widget.ListView.fillDown(ListView.java:710)
        at android.widget.ListView.fillFromTop(ListView.java:771)
        at android.widget.ListView.layoutChildren(ListView.java:1695)
        at android.widget.AbsListView.onLayout(AbsListView.java:2230)
nick
  • 61
  • 1
  • 3
  • 10

3 Answers3

0

As logcat shows, the null array is "imageStream", not the "imageList". And the error comes from this line

ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);

You print "outImage" and you may know what the error.

Mark Shen
  • 346
  • 1
  • 5
0

The key log info is this line:

java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:60)

Let's check the source of ByteArrayInputStream.java

public ByteArrayInputStream(byte buf[]) {
    this.buf = buf;
    this.pos = 0;
    this.count = buf.length;
}

As you can see the code of this.count = buf.length; will get the buf's length. So the param buf is null.

Please check the outImage in line ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);

Hope it help.

JohnWatsonDev
  • 1,227
  • 9
  • 16
0

In getView() method

public View getView(int position, View convertView, ViewGroup arg2) {

    byte[] outImage=imagelistItems.getImage();  <---- outImage = null

Because contructor of ByteArrayInputStream is not allow to pass a null value. so the exception was throw.

Check your input again:

private void offlinemodes() {
    db=new DatabaseHandler(getApplicationContext());
    list = new ArrayList<ImagelistItems>();
    list=db.getAllLabels();       <------- your input

Perhaps you do wrong in getAllLabels() method so the input was wrong consequently.

Alex Hong
  • 281
  • 1
  • 11