我希望有人可以帮助我,因为我遇到了这个错误并且日志对我没有帮助!我正在尝试从我的数据库中读取数据,我知道该数据库包含我提取的表和数据。这行似乎有错误:
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TABLE_SORTBY, null);
这很奇怪,因为我以前使用过这段代码,但我看不出我做错了什么,任何帮助将不胜感激。
/** Get all locations for the dialog box */
public ArrayList<String> allLocations() {
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TABLE_SORTBY, null);
ArrayList<String> mArrayList = new ArrayList<String>();
try {
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
mArrayList.add(mCursor.getString(mCursor.getColumnIndex(SORTBY_SORTBY_TYPE)));
mCursor.moveToNext();
}
} catch (SQLiteException e) {
Log.e("All Locations", "Error getting locations: " + e.toString());
}finally{
mCursor.close();
close();
}
return mArrayList;
}
我得到的错误是:
08-11 14:18:00.465: E/ERROR(19396): java.lang.NullPointerException
08-11 14:18:00.465: W/System.err(19396): java.lang.NullPointerException
08-11 14:18:00.465: W/System.err(19396): at com.swiftdrink.sqlite.SortBySQL.allLocations(SortBySQL.java:99)
08-11 14:18:00.465: W/System.err(19396): at com.swiftdrink.SortByList.displaySortByList(SortByList.java:127)
08-11 14:18:00.525: D/dalvikvm(19396): GC_FOR_MALLOC freed 2629 objects / 147472 bytes in 51ms
08-11 14:18:00.525: W/System.err(19396): at com.swiftdrink.SortByList.downloadSortBy(SortByList.java:116)
08-11 14:18:00.525: W/System.err(19396): at com.swiftdrink.SortBy.onCreate(SortBy.java:58)
08-11 14:18:00.525: W/System.err(19396): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-11 14:18:00.525: W/System.err(19396): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
08-11 14:18:00.525: W/System.err(19396): at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
08-11 14:18:00.535: W/System.err(19396): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
08-11 14:18:00.535: W/System.err(19396): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
08-11 14:18:00.535: W/System.err(19396): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
08-11 14:18:00.535: W/System.err(19396): at android.widget.TabHost.setCurrentTab(TabHost.java:323)
08-11 14:18:00.535: W/System.err(19396): at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
08-11 14:18:00.535: W/System.err(19396): at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
08-11 14:18:00.535: W/System.err(19396): at android.view.View.performClick(View.java:2408)
08-11 14:18:00.535: W/System.err(19396): at android.view.View$PerformClick.run(View.java:8816)
08-11 14:18:00.535: W/System.err(19396): at android.os.Handler.handleCallback(Handler.java:587)
08-11 14:18:00.535: W/System.err(19396): at android.os.Handler.dispatchMessage(Handler.java:92)
08-11 14:18:00.535: W/System.err(19396): at android.os.Looper.loop(Looper.java:123)
08-11 14:18:00.535: W/System.err(19396): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-11 14:18:00.535: W/System.err(19396): at java.lang.reflect.Method.invokeNative(Native Method)
08-11 14:18:00.545: W/System.err(19396): at java.lang.reflect.Method.invoke(Method.java:521)
08-11 14:18:00.545: W/System.err(19396): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-11 14:18:00.545: W/System.err(19396): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-11 14:18:00.545: W/System.err(19396): at dalvik.system.NativeStart.main(Native Method)
如果您想知道这是我的整个 SQL 课程。
public class SortBySQL {
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private final Context mCtx;
private static final String sortby = ("CREATE TABLE " + TABLE_SORTBY
+ " (" + SORTBY_SORTBY_ID + " INT," + SORTBY_SORTBY_TYPE + " 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(sortby);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_NAME);
onCreate(db);
}
}
public SortBySQL(Context ctx) {
this.mCtx = ctx;
}
public SortBySQL open() throws SQLException {
this.mDbHelper = new DatabaseHelper(mCtx);
this.mDb = this.mDbHelper.getWritableDatabase();
return this;
}
public void close() {
this.mDbHelper.close();
}
/** Add to the sort by list */
public long addSortBy(String sortByID, String sortByType) {
Log.d("addLocation", "FUCK");
Log.d("Add Location", sortByID + " " + sortByType);
ContentValues values = new ContentValues();
values.put(SORTBY_SORTBY_ID, sortByID);
values.put(SORTBY_SORTBY_TYPE, sortByType);
return this.mDb.insert(TABLE_SORTBY , null, values);
}
/** Get all locations for the dialog box */
public ArrayList<String> allLocations() {
Cursor mCursor = mDb.rawQuery("SELECT * FROM " + TABLE_SORTBY, null);
ArrayList<String> mArrayList = new ArrayList<String>();
try {
mCursor.moveToFirst();
while (!mCursor.isAfterLast()) {
mArrayList.add(mCursor.getString(mCursor.getColumnIndex(SORTBY_SORTBY_TYPE)));
mCursor.moveToNext();
}
} catch (SQLiteException e) {
Log.e("All Locations", "Error getting locations: " + e.toString());
}finally{
mCursor.close();
close();
}
return mArrayList;
}
/** Delete all from the location table */
public void deleteAllSortBy() {
Log.d("Delete Locations", "Delete Locations");
mDb.delete(TABLE_SORTBY, null, null);
close();
}
/** Check that there are locations stored */
public boolean countSortBy() {
Log.e("countLocation", "countLocation");
Cursor count = mDb.rawQuery("SELECT COUNT(*) FROM " + TABLE_SORTBY,
null);
if (count == null) {
return false;
}
count.close();
close();
return true;
}
}