Search code examples
android-sqliteandroid-imageviewsqliteopenhelper

showing error to get length of null array error for Image view


my problem is when I run the code the app jumps out the application.I am using SQLITE Database using Recycler View I have two view control 1 is Textview and another is Image view.An application running on Textview but when I use Image view then the Android Studio it has the error: "java.lang.NullPointerException: Attempt to get length of null array"

Thanx in advance. here is my Recycler_List_Item_Adapter2.java

@Override
public void onBindViewHolder(MyViewHolder_item holder, int position) {
    Recycler_List_Item_Pojo2 recycler_title_pojo=arrayList.get(position);
    holder.outImage=recycler_title_pojo.getImg();
    ByteArrayInputStream imageStream = new ByteArrayInputStream(holder.outImage);
    Bitmap theImage = BitmapFactory.decodeStream(imageStream);
    holder.imageView.setImageBitmap(theImage);
    holder.textView.setText(recycler_title_pojo.getBr_title());
}


@Override
public int getItemCount() {
    return arrayList.size();
}

public class MyViewHolder_item extends RecyclerView.ViewHolder {
    TextView textView;
    CircleImageView imageView;
    byte[] outImage;

here is my Recycler_List_Item_Pojo2.java

public class Recycler_List_Item_Pojo2 {
byte[] img=null;
String br_title,bt_material,br_procedure,br_notice;

public Recycler_List_Item_Pojo2(byte[] img, String br_title) {
    this.img = img;
    this.br_title = br_title;
}

public byte[] getImg() {
    return img;
}

public void setImg(byte[] img) {
    this.img = img;
}

public String getBr_title() {
    return br_title;
}

public void setBr_title(String br_title) {
    this.br_title = br_title;
}

Here is my Sqlite Database code

Recycler_List_Item_Pojo2 product = null;
    List<Recycler_List_Item_Pojo2> productList = new ArrayList<Recycler_List_Item_Pojo2>();
    openDatabase();
    Cursor cursor = mDatabase.rawQuery(" select * from a", null, null);
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {

        product = new Recycler_List_Item_Pojo2(cursor.getBlob(1),cursor.getString(2));
        productList.add(product);
        cursor.moveToNext();
    }
    cursor.close();
    closeDatabase();
    return productList;

Here is my error

FATAL EXCEPTION: main
                                                            Process: com.pp.receipe, PID: 21550
                                                            java.lang.NullPointerException: Attempt to get length of null array
                                                                at java.io.ByteArrayInputStream.<init>(ByteArrayInputStream.java:60)
                                                                at com.pp.receipe.adapter.Recycler_List_Item_Adapter2.onBindViewHolder(Recycler_List_Item_Adapter2.java:50)
                                                                at com.pp.receipe.adapter.Recycler_List_Item_Adapter2.onBindViewHolder(Recycler_List_Item_Adapter2.java:30)

Solution

  • @Prashant Please check your SQLITE Database.If any NULL value is inserted or check any field is empty or not.Remove your empty field from database.