後でダイアログボックスを作成するために使用できる配列リストにDBから場所のリストを取得しようとしていますが、以下のメソッドを呼び出すと、以下のエラーが発生する可能性があります。誰かが私が間違っていることと、それをキャッチしているときにキャッチされない例外が発生する理由を誰かが見ることができますか?私は本当に助けていただければ幸いです!
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());
}
}
}