突然间,所有数据库功能都无法在我的应用上运行。错误始终为NullPointer exception
,并指向来自dataObjectsProvider class
的不同方法,取决于所使用的功能。例如,当尝试在CreateDataObjectsMethod
上插入值时,尝试清空/删除数据库为deleteObjectsMethod
时。
所有这些功能都是在我最后一次关闭eclipse时工作的,我很确定除了onClick
更新按钮方法之外我没有改变任何其他内容,我试图让它工作。这并不能解释为什么所有其他功能都不起作用。你能检查我是否意外地犯了一个我看不到的错误吗?
DataObjectsProvider类:
public class DataObjectsProvider {
// Database fields
private SQLiteDatabase database;
private DbHelper dbHelper;
private String[] allColumns = { DbHelper.COLUMN_ID,
DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST,
DbHelper.COLUMN_SONGGENRE };
public DataObjectsProvider (Context context) {
dbHelper = new DbHelper(context);
}
public void open() throws SQLException {
dbHelper.getWritableDatabase();
}
public void close() {
dbHelper.close();
}
public DataObjects createDataObjects (String songName, String songArtist,
String songGenre) {
dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_SONGNAME, songName);
values.put(DbHelper.COLUMN_SONGARTIST, songArtist);
values.put(DbHelper.COLUMN_SONGGENRE, songGenre);
long insertId = database.insert(DbHelper.TABLE_SONGS, null,
values);
Cursor cursor = database.query(DbHelper.TABLE_SONGS,
allColumns, DbHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
DataObjects newDobject = cursorToDataObjects(cursor);
cursor.close();
return newDobject;
}
public Cursor fechAllDobjects(){
Cursor mCursor = database.query(DbHelper.TABLE_SONGS,
new String[] {DbHelper.COLUMN_ID,
DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST,
DbHelper.COLUMN_SONGGENRE},
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public Cursor fechDobjectsByName(String inputText) throws SQLException {
Cursor mCursor = null;
if (inputText == null || inputText.length () == 0) {
mCursor = database.query(DbHelper.TABLE_SONGS,
new String[] {DbHelper.COLUMN_ID,
DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST,
DbHelper.COLUMN_SONGGENRE},
null, null, null, null, null);
}
else {
mCursor = database.query(true, DbHelper.TABLE_SONGS,
new String[] {DbHelper.COLUMN_ID,
DbHelper.COLUMN_SONGNAME, DbHelper.COLUMN_SONGARTIST,
DbHelper.COLUMN_SONGGENRE},
DbHelper.COLUMN_SONGNAME + " like '%" + inputText + "%'", null,
null, null, null, null);
}
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
/* public void deleteDobjects(DataObjects dobjects) {
long id = dobjects.getId();
database.delete(DbHelper.TABLE_SONGS, DbHelper.COLUMN_ID
+ " = " + id, null);
}*/
public void DeleteItem(long l)
{
dbHelper.getWritableDatabase();
String query = "DELETE FROM songs WHERE _id="+l;
database.execSQL(query);
}
public void UpdateItem(int id, String songName, String songArtist, String songGenre) {
dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_SONGNAME, songName);
values.put(DbHelper.COLUMN_SONGARTIST, songArtist);
values.put(DbHelper.COLUMN_SONGGENRE, songGenre);
database.update(DbHelper.TABLE_SONGS, values, DbHelper.COLUMN_ID +"="+id, null);
}
public void deleteAllDobjects() {
dbHelper.getWritableDatabase();
database.delete(DbHelper.TABLE_SONGS, null, null);
}
public List<DataObjects> getAlldobjects() {
List<DataObjects> dobjects = new ArrayList<DataObjects>();
Cursor cursor = database.query(DbHelper.TABLE_SONGS,
allColumns, null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
DataObjects dobject = cursorToDataObjects(cursor);
dobjects.add(dobject);
cursor.moveToNext();
}
// Make sure to close the cursor
cursor.close();
return dobjects;
}
private DataObjects cursorToDataObjects(Cursor cursor) {
DataObjects dobject = new DataObjects();
dobject.setId(cursor.getLong(0));
dobject.setSongName(cursor.getString(1));
dobject.setSongArtist(cursor.getString(2));
dobject.setSongGenre(cursor.getString(3));
return dobject;
}
}
数据库助手类:
public class DbHelper extends SQLiteOpenHelper {
public static final String TABLE_SONGS = "songs";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_SONGNAME = "songname";
public static final String COLUMN_SONGARTIST = "artist";
public static final String COLUMN_SONGGENRE = "genre";
private static final String DATABASE_NAME = "songsdb.db";
private static final int DATABASE_VERSION = 1;
// Database creation sql statement
private static final String DATABASE_CREATE = "create table "
+ TABLE_SONGS + "("
+ COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_SONGNAME + ","
+ COLUMN_SONGARTIST + ","
+ COLUMN_SONGGENRE + ");";
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
Log.w(DbHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
database.execSQL("DROP TABLE IF EXISTS " + TABLE_SONGS);
onCreate(database);
}
}
删除数据库日志:
02-15 13:33:13.158: W/dalvikvm(1966): threadid=1: thread exiting with uncaught exception (group=0x40c931f8)
02-15 13:33:13.168: E/AndroidRuntime(1966): FATAL EXCEPTION: main
02-15 13:33:13.168: E/AndroidRuntime(1966): java.lang.NullPointerException
02-15 13:33:13.168: E/AndroidRuntime(1966): at com.d69.dancesongsdb.DataObjectsProvider.deleteAllDobjects(DataObjectsProvider.java:113)
02-15 13:33:13.168: E/AndroidRuntime(1966): at com.d69.dancesongsdb.MainActivity$3.onClick(MainActivity.java:102)
02-15 13:33:13.168: E/AndroidRuntime(1966): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
02-15 13:33:13.168: E/AndroidRuntime(1966): at android.os.Handler.dispatchMessage(Handler.java:99)
02-15 13:33:13.168: E/AndroidRuntime(1966): at android.os.Looper.loop(Looper.java:137)
02-15 13:33:13.168: E/AndroidRuntime(1966): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-15 13:33:13.168: E/AndroidRuntime(1966): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 13:33:13.168: E/AndroidRuntime(1966): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 13:33:13.168: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-15 13:33:13.168: E/AndroidRuntime(1966): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
02-15 13:33:13.168: E/AndroidRuntime(1966): at dalvik.system.NativeStart.main(Native Method)
插入数据日志:
02-15 13:35:36.457: W/dalvikvm(2104): threadid=1: thread exiting with uncaught exception (group=0x40c931f8)
02-15 13:35:36.477: E/AndroidRuntime(2104): FATAL EXCEPTION: main
02-15 13:35:36.477: E/AndroidRuntime(2104): java.lang.NullPointerException
02-15 13:35:36.477: E/AndroidRuntime(2104): at com.d69.dancesongsdb.DataObjectsProvider.createDataObjects(DataObjectsProvider.java:39)
02-15 13:35:36.477: E/AndroidRuntime(2104): at com.d69.dancesongsdb.MainActivity$2.onClick(MainActivity.java:66)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.view.View.performClick(View.java:3511)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.view.View$PerformClick.run(View.java:14105)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.os.Handler.handleCallback(Handler.java:605)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.os.Handler.dispatchMessage(Handler.java:92)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.os.Looper.loop(Looper.java:137)
02-15 13:35:36.477: E/AndroidRuntime(2104): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-15 13:35:36.477: E/AndroidRuntime(2104): at java.lang.reflect.Method.invokeNative(Native Method)
02-15 13:35:36.477: E/AndroidRuntime(2104): at java.lang.reflect.Method.invoke(Method.java:511)
02-15 13:35:36.477: E/AndroidRuntime(2104): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:787)
02-15 13:35:36.477: E/AndroidRuntime(2104): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:554)
02-15 13:35:36.477: E/AndroidRuntime(2104): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您只需要几行代码:
dbHelper.getWritableDatabase();
现在,这实际上不会做任何事情。相反,我认为你想这样做:
database = dbHelper.getWritableDatabase();
如果没有该行,database
在null
createDataObjects
中使用时会导致NullPointerException
。{/ p>
我至少计算了5次发生这种情况的情况,所以请确保全部更改。