Android Array List未捕获异常

时间:2012-06-05 03:43:22

标签: android arrays exception

我正在尝试从我的数据库中提取一个位置列表到一个数组列表,我可以在以后使用它来创建一个对话框,但是当我调用下面的方法时,我可以看到下面的错误。任何人都可以看到我做错了什么以及为什么我抓到它时会得到一个未捕获的异常?我真的很感激你的帮助!

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());
    }
}

//错误

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)

//全班

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());
        }
    }
}

1 个答案:

答案 0 :(得分:2)

mDb至少在此处显示的代码中为空。

你有其他一些初始化mDb参考的代码吗?

编辑:]

实际上可能需要在使用此类的Activity类中进行更改。

在任何其他方法之前,您需要确保在您的活动中调用location.open()。