James
James

Reputation: 474

Android Array List uncaught exception

I'm trying to pull in a list of locations from my DB into an Array list which I can later use to create a dialog box but when I call the below method I can the errors below. Can anyone see what I'm doing wrong and why I get an uncaught exception when I am catching it? I would really appreciate the help!

public void allLocations() {

    try{
        Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TABLE_LOCATION, null);
        ArrayList<String> mArrayList = new ArrayList<String>();
        mCursor.moveToFirst();
        while(!mCursor.isAfterLast()) {
             mArrayList.add(mCursor.getString(mCursor.getColumnIndex(LOCATION)));
             mCursor.moveToNext();
        }
    }catch (SQLiteException e){
        Log.e("All Locations", "Error getting locations: " + e.toString());
    }
}

// Errors

06-05 03:36:29.345: D/dalvikvm(2110): GC_EXTERNAL_ALLOC freed 655 objects / 51272 bytes in 77ms
06-05 03:36:30.185: V/one(2110): locationSelection
06-05 03:36:31.805: D/AndroidRuntime(2110): Shutting down VM
06-05 03:36:31.805: W/dalvikvm(2110): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
06-05 03:36:31.825: E/AndroidRuntime(2110): FATAL EXCEPTION: main
06-05 03:36:31.825: E/AndroidRuntime(2110): java.lang.NullPointerException
06-05 03:36:31.825: E/AndroidRuntime(2110):     at com.myapp.sqlite.Location.allLocations(Location.java:137)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at com.myapp.Options.displayLocations(Options.java:178)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at com.myapp.Options$1.onClick(Options.java:69)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.view.View.performClick(View.java:2408)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.view.View$PerformClick.run(View.java:8816)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.os.Handler.handleCallback(Handler.java:587)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.os.Looper.loop(Looper.java:123)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at java.lang.reflect.Method.invokeNative(Native Method)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at java.lang.reflect.Method.invoke(Method.java:521)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-05 03:36:31.825: E/AndroidRuntime(2110):     at dalvik.system.NativeStart.main(Native Method)

//The whole class

public class Location {

    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;
    private final Context mCtx;

    private static final String location = ("CREATE TABLE " + TABLE_LOCATION
            + " (" + LOCATION + " TEXT," + LOCATION_ID + " TEXT " + ");");

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(location);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
            onCreate(db);

        }
    }

    public Location(Context ctx) {
        this.mCtx = ctx;
    }

    public Location open() throws SQLException {
        this.mDbHelper = new DatabaseHelper(this.mCtx);
        this.mDb = this.mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        this.mDbHelper.close();
    }

    public long addLocation(String locations, String location_id) {

        ContentValues values = new ContentValues();

        values.put(LOCATION, locations);
        values.put(LOCATION_ID, location_id);

        return this.mDb.insert(TABLE_LOCATION, null, values);
    }

    public String getLocation(long l) throws SQLException {

        String[] columns = new String[] { LOCATION, LOCATION_ID };
        Cursor c = mDb.query(TABLE_LOCATION, columns, null, null, null, null,
                null);
        String result = "";

        int gLocation = c.getColumnIndex(LOCATION);
        int gLocationID = c.getColumnIndex(LOCATION_ID);

        for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
            result = result + c.getString(gLocation) + " "
                    + c.getString(gLocationID) + " " + "\n";
        }
        return result;
    }

    /** Delete all from the location table */
    public void deleteAllLocations() {
        mDb.delete(TABLE_LOCATION, null, null);
    }

    /** Check that there are locations stored */
    public boolean countLocation() {

        Cursor count = mDb.rawQuery("SELECT COUNT(*) FROM " + TABLE_LOCATION,
                null);

        if (count == null) {
            return true;
        }
        return false;
    }

    public void allLocations() {

        try{
            Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TABLE_LOCATION, null);
            ArrayList<String> mArrayList = new ArrayList<String>();
            mCursor.moveToFirst();
            while(!mCursor.isAfterLast()) {
                 mArrayList.add(mCursor.getString(mCursor.getColumnIndex(LOCATION)));
                 mCursor.moveToNext();
            }
        }catch (SQLiteException e){
            Log.e("All Locations", "Error getting locations: " + e.toString());
        }
    }
}

Upvotes: 0

Views: 175

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

mDb is null at least in the code shown here.

do you have some other code where you are initializing the mDb reference?

EDIT :]

The change actually probably needs to be made in the Activity class that is using this class.

You need to make sure in your activity you are calling location.open(), before any other methods.

Upvotes: 2

Related Questions