2012-12-22 74 views
0

SQLite中排在我的應用程序有一個數據庫,在我有一個包含3個columns.I 1代表要刪除表中的行BU使用該表的ID ..在delete一個機器人

我使用此代碼:

public long DeleteStory(int rowId) 
    { 
     System.out.println("pos="+rowId); 
     long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null); 
     System.out.println("edl="+del); 
     return del; 
    } 

我的數據庫中包含的數據:

id    heading     Description 
1    fgghsgahs    tyfgjhdfsdks 

但它顯示爲空指針exception..I嘗試了很多,,但我不能能解決它。幫我解決這個問題..提前感謝。

我的logcat如下:

12-22 10:34:50.796: I/System.out(1207): pppppppp=1 
12-22 10:34:50.816: I/System.out(1207): pos=1 
12-22 10:34:50.816: D/AndroidRuntime(1207): Shutting down VM 
12-22 10:34:50.816: W/dalvikvm(1207): threadid=3: thread exiting with uncaught exception (group=0x4001b188) 
12-22 10:34:50.816: E/AndroidRuntime(1207): Uncaught handler: thread main exiting due to uncaught exception 
12-22 10:34:50.836: E/AndroidRuntime(1207): java.lang.NullPointerException 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.newspaper.DBadapter.DeleteStory(DBadapter.java:89) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.newspaper.SavedStoriesActivity.onOptionsItemSelected(SavedStoriesActivity.java:64) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.app.Activity.onMenuItemSelected(Activity.java:2170) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:730) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:139) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:855) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.view.menu.IconMenuView.invokeItem(IconMenuView.java:525) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.view.menu.IconMenuItemView.performClick(IconMenuItemView.java:122) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.view.View.onTouchEvent(View.java:4179) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.widget.TextView.onTouchEvent(TextView.java:6541) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.view.View.dispatchTouchEvent(View.java:3709) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1691) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.os.Handler.dispatchMessage(Handler.java:99) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.os.Looper.loop(Looper.java:123) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at android.app.ActivityThread.main(ActivityThread.java:4363) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at java.lang.reflect.Method.invoke(Method.java:521) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
12-22 10:34:50.836: E/AndroidRuntime(1207):  at dalvik.system.NativeStart.main(Native Method) 

回答

1

的logcat中顯示,該行被拋出NullPointerException異常:

long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null); 

這意味着dbnull。數據庫適配器中是否有open()方法?您需要在您的SQLiteOpenHelper對象上調用getWritableDatabase()來初始化db

+0

感謝答案..我做了瘋狂的事情..我沒有打開數據庫B4 accesing that..Now我得到了答案.. +1時間花了我.. – Subburaj

1

你忘了做數據庫操作,從而改變你的代碼之前初始化數據庫:數據庫

public long DeleteStory(int rowId) 
    { 
     SQLiteDatabase db = this.getWritableDatabase(); 
     System.out.println("pos="+rowId); 
     long del=db.delete(DATABASE_TABLE, KEY_ROWID + "="+rowId,null); 
     System.out.println("edl="+del); 
     db.close(); 
     return del; 
    } 

更多的幫助,刪除,插入,更新操作爲:

http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

+0

謝謝..很多.. +1花費的時間對我來說.. – Subburaj