Search code examples
androidsqlitenullpointerexceptionuniversal-image-loader

NullPointerException when retrieving data from sqlite database


ive stored some image names in a sqlite database.My code is supposed to retrieve all the imagenames and store it in a array and then assign the first image to an imageview based on its name.Im using UIL to set the images to the image view.

MY CODE

public class Locker extends Activity {
int index = 0, limit, tolerance = 40, state = 1;
String[] bgimg;
String[] ranimg;
int[] ActXCod;
int[] ActYCod;
ImageView image;
ImageLoader imageloader;
File Path;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.locker);
    image = (ImageView) findViewById(R.id.imageView1);
    SharedPreferences getprefs = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    String temp = getprefs.getString("ImageSeqNo", "4");
    limit = Integer.parseInt(temp);
    Path = getExternalFilesDir(null);
    Constants cons = new Constants();
    ranimg = cons.getImagePath(Path.toString());
    imageloader = ImageLoader.getInstance();
    try {
        new Thread(new getData()).start();

        imageloader.displayImage("file://" + Path.toString() + "/"
                + bgimg[index], image);
    } catch (Exception e) {
        String error = e.toString();
        Dialog d = new Dialog(this);
        d.setTitle("ERROR");
        TextView tv = new TextView(this);
        tv.setText(error);
        d.setContentView(tv);
        d.show();
    }

}

public boolean onTouchEvent(MotionEvent event) {
    // MotionEvent object holds X-Y values
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        int XCood = (int) event.getX();
        int YCood = (int) event.getY();
        int XMin = ActXCod[index] - tolerance, XMax = ActXCod[index]
                + tolerance, YMin = ActYCod[index] - tolerance, YMax = ActYCod[index]
                + tolerance;

        /*
         * String text = "You clicked at x = " + XCood + " and y = " +
         * YCood; final Toast toast = Toast.makeText(this, text,
         * Toast.LENGTH_SHORT); toast.show(); Handler handler = new
         * Handler(); handler.postDelayed(new Runnable() {
         * 
         * @Override public void run() { toast.cancel(); } }, 750);
         */

        if (index < (limit - 1)) // loop to check number of images
        {
            // loop to check status
            if ((state == 1)
                    && (((XCood > XMin) && (XCood < XMax)) && ((YCood > YMin) && (YCood < YMax)))) {
                index++;
                imageloader.displayImage("file://" + Path.toString() + "/"
                        + bgimg[index], image);
            } else {
                index++;
                Random r = new Random();
                int ran = r.nextInt(10 - 0);
                imageloader.displayImage("file://" + ranimg[ran], image);
                state = 0;
            }
        } else {
            if (state == 1) {
                Intent i = new Intent(Locker.this, Compare_Pattern.class);
                startActivity(i);
                finish();
            } else {
                new AlertDialog.Builder(this)
                        .setTitle("Failed Login Attempt")
                        .setMessage(
                                "Your previous login attempt has failed.Would you like to try again?")
                        .setPositiveButton(android.R.string.yes,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        // continue with delete
                                        Intent intent = getIntent();

                                        startActivity(intent);
                                        finish();
                                    }
                                })
                        .setNegativeButton(android.R.string.no,
                                new DialogInterface.OnClickListener() {
                                    public void onClick(
                                            DialogInterface dialog,
                                            int which) {
                                        // do nothing
                                        finish();
                                    }
                                }).show();
            }
        }

    }

    return super.onTouchEvent(event);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class getData implements Runnable {

    @Override
    public void run() {
        // TODO Auto-generated method stub
        DataBaseHandler handler = new DataBaseHandler(
                getApplicationContext());
        handler.open();
        ActXCod = handler.getXcod();
        ActYCod = handler.getYcod();
        bgimg = handler.getImg();
        handler.close();

    }

}

}

i tried debugging the code and its giving me a Java.lang.NullPointerException for bgimg[index] at the line imageloader.displayImage("file://" + Path.toString() + "/" + bgimg[index], image);. i cannot figure out why. Any help is gratefully accepted.


Solution

  • new Thread(new getData()).start();
    imageloader.displayImage("file://" + Path.toString() + "/"
                + bgimg[index], image);
    

    bgimg is not initialized yet. Even if you start your thread, it does not run and compelete initialization of bgimg immediately.

    Move the code that requires the result of the thread code into the thread itself. You can use Activity.runOnUiThread() to move the execution back to UI thread from a background thread.