Question

i get string theme_name in database sqlite and then use theme name to set a image in gallery . But i have some error with nullpointer exception. Can you tell me why happen ? and how can i do ? Thank you

this is my code

 public class MybookActivity_Gallery extends Activity{

private BooksDB db;
private Context context;
private HashMap< String, ArrayList<String>> hm;
private ImageView imgOldSelected;
private int resID;
private ImageView img_shelf;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout_mybookactivity_gallery);
    context = this;
    db = new BooksDB(context);
    ArrayList<String> list_bokID = new ArrayList<String>();
    ArrayList<String> list_title = new ArrayList<String>();
    ArrayList<String> list_theme = new ArrayList<String>();
    hm = new HashMap<String, ArrayList<String>>();

    //get Theme_name
    hm = db.getBookTheme();
    if(hm.isEmpty() == true){
        System.out.println("not have data");
    }else{
        //prepare theme, title, book_id 
        list_theme = hm.get("theme");
        list_title = hm.get("title");
        list_bokID = hm.get("bokID");
    }

    LinearLayout gallery = (LinearLayout)findViewById(R.id.images_gallery);
    //image fullsize 
    final ImageView imgFullSize = (ImageView)findViewById(R.id.full_size_image);

    for (String themeName : list_theme) {
        //prepare image in layout gallery 
        ImageView img_shelf = new ImageView(this);
        img_shelf.setLayoutParams(new LinearLayout.LayoutParams(150, 150));
        img_shelf.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        img_shelf.setPadding(8, 8, 8, 8);
        //set image resource
        resID = getResources().getIdentifier(themeName, "drawable", getPackageName());
        img_shelf.setImageResource(resID);


    }

    // img_shelf set it in on click
                img_shelf.setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        ImageView selectedImage = (ImageView) v;
                        Bitmap bitmap = ((BitmapDrawable) selectedImage.getDrawable()).getBitmap();

                        if(imgOldSelected != null){
                            imgOldSelected.setBackgroundColor(Color.TRANSPARENT);
                        }
                        selectedImage.setBackgroundColor(Color.LTGRAY);

                    }
                });

                gallery.addView(img_shelf);
}

}

this is my logcat error

 02-21 16:12:02.421: E/AndroidRuntime(29116): FATAL EXCEPTION: main
 02-21 16:12:02.421: E/AndroidRuntime(29116): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.splash.bookguk_project/category_viewbook_sub2_1.MybookActivity_Gallery}: java.lang.NullPointerException
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread.access$700(ActivityThread.java:140)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.os.Handler.dispatchMessage(Handler.java:99)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.os.Looper.loop(Looper.java:137)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread.main(ActivityThread.java:4921)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at java.lang.reflect.Method.invokeNative(Native Method)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at java.lang.reflect.Method.invoke(Method.java:511)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at dalvik.system.NativeStart.main(Native Method)
 02-21 16:12:02.421: E/AndroidRuntime(29116): Caused by: java.lang.NullPointerException
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at category_viewbook_sub2_1.MybookActivity_Gallery.onCreate(MybookActivity_Gallery.java:73)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.Activity.performCreate(Activity.java:5206)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
 02-21 16:12:02.421: E/AndroidRuntime(29116):   ... 11 more

thank you

Was it helpful?

Solution 2

You defined img_shelf variable on the class and then you redefine it again in the loop (so it's instantiated only within the scope of the loop):

for (String themeName : list_theme) {
    //prepare image in layout gallery 
    ImageView img_shelf = new ImageView(this);
    ...
}

This won't work as intended.

I'm not sure how you wanted to use it though as it seems that you create multiple ImageView's but then attach OnClickListener to only one of them. You will need to either move the code for OnClickListener into the loop or create a variable - list of ImageView on the class.

OTHER TIPS

You have this ImageView delcared an initialized inside for loop

   for (String themeName : list_theme) {
    //prepare image in layout gallery 
    ImageView img_shelf = new ImageView(this);
    //this iamgeview is not added to gridview or to the layout
   }

So you have a NullPointerException @

   img_shelf.setOnClickListener(new View.OnClickListener() { // outside for loop

coz

   private ImageView img_shelf;  // is declared not initialized
   @Override
   protected void onCreate(Bundle savedInstanceState) {

Also the ImageView declared and initialized in for loop is not added to gridview or activity.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top