mercredi 4 février 2015

Android -Arrayindexbound and NullPointer Exception in setting a byte[] of image from database to Listview

I need to insert and retrieve these from SQLite database and set to image view in a custom Listview.But Running the app throws an ArrayIndexOutOfBound Exception with length and index=8 in line img[j] = imge;.SO I tried making j=0 in while loop .But it causes a null point exception in line : Bitmap b1=BitmapFactory.decodeByteArray(img[j], 0, img.length);.What should I do. I have images in array.Then why is it showing Null.


MainActivity



public class MainActivity extends Activity{


ListView prd_list;
public static Integer clas;
byte [] imge;

int j;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
prd_list = (ListView) findViewById(R.id.list);
DataBaseHandler dbh = new DataBaseHandler(this);


Bitmap[] images = { BitmapFactory.decodeResource(getResources
(),R.drawable.candle1),BitmapFactory.decodeResource(getResources
(),R.drawable.candl3),BitmapFactory.decodeResource(getResources(),R.drawable.candl4),BitmapFactory.decodeResource(getResources(),R.drawable.candl5),BitmapFactory.decodeResource(getResources(),R.drawable.candl6),BitmapFactory.decodeResource(getResources(),R.drawable.sglc10),BitmapFactory.decodeResource(getResources(),R.drawable.senson),BitmapFactory.decodeResource(getResources(),R.drawable.lawn)};

byte[][] img = new byte[images.length][];
for (j=0; j<images.length; j++) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
images[j].compress(Bitmap.CompressFormat.PNG, 100, bos);

// use a 2D array for img if you want to retain the byte arrays for all the bitmaps
img[j] = bos.toByteArray();
}


SQLiteDatabase db = dbh.getWritableDatabase();
Cursor cr = db.rawQuery("SELECT * FROM product", null);
final String[] pname = new String[cr.getCount()];
String[] price = new String[cr.getCount()];

int i = 0;
while(cr.moveToNext())
{
// j=0
String name = cr.getString(cr.getColumnIndex("pname"));
String prprice = cr.getString(cr.getColumnIndex("pprice"));

imge=cr.getBlob(cr.getColumnIndex("pimage"));
img[j] = imge;

pname[i] = name;
price[i] = prprice;
i++;
}
ListAdapter adapter = new ListAdapter(this, img,pname, price);
prd_list.setAdapter(adapter);

}
}


ListAdapter



public class ListAdapter extends BaseAdapter {

private final String[] pname;
private final String[] price;
private final byte[] []img;
private Context mcontext;
int j;

public ListAdapter(Context c,byte[][]img,String[] pname,String[] price){
mcontext=c;
this.pname=pname;
this.price=price;
this.img=img;
}

@Override
public int getCount() {
// TODO Auto-generated method stub
return pname.length;
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View List;
LayoutInflater mLayoutinflater=(LayoutInflater) mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
List=new View(mcontext);

List=mLayoutinflater.inflate(R.layout.mylist, parent, false);
}
else{
List = (View)convertView;
}

TextView textView1 = (TextView)List.findViewById(R.id.pr_name);
TextView textView2 = (TextView)List.findViewById(R.id.pr_price);
ImageView imageview= (ImageView)List.findViewById(R.id.pr_img);
textView1.setText(pname[position].toString());
textView2.setText("Rs "+price[position] +" /-");
Bitmap b1=BitmapFactory.decodeByteArray(img[j], 0, img.length);
imageview.setImageBitmap(b1);

// TODO Auto-generated method stub
return List;

}

}


LOGCAT



02-05 01:45:36.657: E/AndroidRuntime(2102): FATAL EXCEPTION: main
02-05 01:45:36.657: E/AndroidRuntime(2102): Process: com.example.bitmap_img, PID: 2102
02-05 01:45:36.657: E/AndroidRuntime(2102): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bitmap_img/com.example.bitmap_img.MainActivity}: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread.access$700(ActivityThread.java:135)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.os.Handler.dispatchMessage(Handler.java:102)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.os.Looper.loop(Looper.java:137)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread.main(ActivityThread.java:4998)
02-05 01:45:36.657: E/AndroidRuntime(2102): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 01:45:36.657: E/AndroidRuntime(2102): at java.lang.reflect.Method.invoke(Method.java:515)
02-05 01:45:36.657: E/AndroidRuntime(2102): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
02-05 01:45:36.657: E/AndroidRuntime(2102): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
02-05 01:45:36.657: E/AndroidRuntime(2102): at dalvik.system.NativeStart.main(Native Method)
02-05 01:45:36.657: E/AndroidRuntime(2102): Caused by: java.lang.ArrayIndexOutOfBoundsException: length=8; index=8
02-05 01:45:36.657: E/AndroidRuntime(2102): at com.example.bitmap_img.MainActivity.onCreate(MainActivity.java:77)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.Activity.performCreate(Activity.java:5243)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-05 01:45:36.657: E/AndroidRuntime(2102): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
02-05 01:45:36.657: E/AndroidRuntime(2102): ... 11 more

Aucun commentaire:

Enregistrer un commentaire