0

我正在尝试将我的数据库中的位置列表拉入一个数组列表,稍后我可以用它来创建一个对话框,但是当我调用下面的方法时,我可以看到下面的错误。谁能看到我做错了什么以及为什么我在捕捉它时会得到一个未捕获的异常?我非常感谢您的帮助!

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

1 回答 1

2

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

您是否有其他代码正在初始化 mDb 引用?

编辑 :]

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

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

于 2012-06-05T03:47:18.193 回答