获取字节数组的nullpointer异常

时间:2016-06-17 07:19:59

标签: android arrays listview 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();
    }
}

错误:

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)

3 个答案:

答案 0 :(得分:0)

如logcat所示,空数组是&#34; imageStream&#34;,而不是&#34; imageList&#34;。错误来自这一行

ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);

你打印&#34; outImage&#34;你可能知道错误是什么。

答案 1 :(得分:0)

关键日志信息是这一行:

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

让我们检查ByteArrayInputStream.java

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

正如您所看到的,this.count = buf.length;的代码将获得buf的长度。所以param buf为空。

请检查outImage

行中的ByteArrayInputStream imageStream = new ByteArrayInputStream(outImage);

希望有所帮助。

答案 2 :(得分:0)

getView()方法

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

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

因为ByteArrayInputStream的构造函数不允许传递空值。所以例外是抛出。

再次检查您的输入:

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

也许你在getAllLabels()方法中做错了所以输入错了。