I am trying to implement a gallery in which I am getting URLs of Images(thumbs) in form of a JSON file. I parsed json into ArrayLists thmb that saves URL's of Thumbnail Images in form of strings. EVERYTHING IS FINE WITH JSONs and the URL strings.
I want to load the thumbnails in gallery view. For this I am trying to Download the Images and save them in ArrayList. The code for the same is:
ArrayList<String> thmb;
ArrayList<Bitmap> b;
DownloadImageTask dit = new DownloadImageTask();
for(int j=0;j<thmb.size();j++){
Log.d("REQ","REQUESTING"+thmb.get(j));
dit.execute(thmb.get(j).toString());
Log.d("BR", "BITMAP RECIEVED");
Bitmap bit = null;
try{
bit=dit.get();
b.add(bit);
}catch(Exception e){
Log.d("LLL", e.getMessage());
}
}
Class Download Image Task:
public class DownloadImageTask extends AsyncTask <String, Bitmap, Bitmap> {
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap img = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
img = BitmapFactory.decodeStream(in);
return img;
} catch (Exception e) {
Log.e("Error", e.getMessage());
return null;
}
}
protected void onPostExecute(Bitmap imx) {
super.onPostExecute(imx);
}
}
I am not able to save the bitmaps in the ArrayList and the app gets stop unexpectedly! Can anyone tell me why this is happening. the LogCat Says:
12-22 18:48:07.398: D/BR(6129): BITMAP RECIEVED
12-22 18:48:08.349: D/AndroidRuntime(6129): Shutting down VM
12-22 18:48:08.349: W/dalvikvm(6129): threadid=1: thread exiting with uncaught exception (group=0x415e9d88)
12-22 18:48:08.359: E/AndroidRuntime(6129): FATAL EXCEPTION: main
12-22 18:48:08.359: E/AndroidRuntime(6129): Process: com.example.videogallery, PID: 6129
12-22 18:48:08.359: E/AndroidRuntime(6129): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.videogallery/com.example.videogallery.Stream}: java.lang.NullPointerException: println needs a message
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2237)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2286)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1246)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.os.Looper.loop(Looper.java:212)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.main(ActivityThread.java:5135)
12-22 18:48:08.359: E/AndroidRuntime(6129): at java.lang.reflect.Method.invokeNative(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): at java.lang.reflect.Method.invoke(Method.java:515)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-22 18:48:08.359: E/AndroidRuntime(6129): at dalvik.system.NativeStart.main(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): Caused by: java.lang.NullPointerException: println needs a message
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.util.Log.println_native(Native Method)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.util.Log.d(Log.java:139)
12-22 18:48:08.359: E/AndroidRuntime(6129): at com.futurristic.videogallery.Stream.onCreate(Stream.java:90)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.Activity.performCreate(Activity.java:5231)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-22 18:48:08.359: E/AndroidRuntime(6129): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2201)
12-22 18:48:08.359: E/AndroidRuntime(6129): ... 11 more
Based on Logcat you have NPE in thmb.get(j), so show the part where it fills.