2012-11-14 91 views
1

因爲,我是新的Android世界所以,請幫助我。當我與Android SQLite的工作從數據庫中檢索數據,這些codes-雖然從數據庫中恢復數據顯示NUll POINTER EXCEPTION

Cursor cursor; 
DatabaseConnector dbConnector = new DatabaseConnector(this); 
private ListAdapter conAdapter; 
String[] from_for_text_in_list = {"name"}; 
int[] to_for_text_in_list = {R.id.title_info_txt_v} 
cursor = dbConnector.getAllContacts(); 
conAdapter = new SimpleCursorAdapter(Infoclass.this, R.layout.listview_layout, null, from_for_text_in_list, to_for_text_in_list); 
setListAdapter(conAdapter); 

現在我宣佈getAllmethod()AS-

public Cursor getAllContacts() 
{ 
    return database.rawQuery("SELECT _id, name FROM tcc_info_table ", new String[]{"_id", "name"}); 
} 

「名」就是數據庫的列名在我的應用程序。

當我運行我的應用程序時,它顯示了getAllContact()方法中的NULL POINTER異常。即使我通過SQLite瀏覽器檢查我的數據庫,我發現數據插入成功。但是數據不能從數據庫檢索到列表視圖。我logcat的錯誤是因爲如下─

  11-14 12:01:21.694: E/Trace(30607): error opening trace file: No such file or directory (2) 

11-14 12:01:22.475: D/AndroidRuntime(30607): Shutting down VM 
11-14 12:01:22.475: W/dalvikvm(30607): threadid=1: thread exiting with uncaught exception (group=0x40a13300) 
11-14 12:01:22.495: E/AndroidRuntime(30607): FATAL EXCEPTION: main 
11-14 12:01:22.495: E/AndroidRuntime(30607): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.ModiForIndia}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.InfoNarendraModi}: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.access$600(ActivityThread.java:130) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.os.Looper.loop(Looper.java:137) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.main(ActivityThread.java:4745) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at java.lang.reflect.Method.invokeNative(Native Method) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at java.lang.reflect.Method.invoke(Method.java:511) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at dalvik.system.NativeStart.main(Native Method) 
11-14 12:01:22.495: E/AndroidRuntime(30607): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.kt.narendramodi/com.kt.narendramodi.InfoNarendraModi}: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.startActivityNow(ActivityThread.java:1900) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.widget.TabHost.setCurrentTab(TabHost.java:358) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.widget.TabHost.addTab(TabHost.java:236) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at com.kt.narendramodi.ModiForIndia.onCreate(ModiForIndia.java:41) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.Activity.performCreate(Activity.java:5008) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
11-14 12:01:22.495: E/AndroidRuntime(30607): ... 11 more 
11-14 12:01:22.495: E/AndroidRuntime(30607): Caused by: java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at com.kt.nanrendramodi.databasehelper.DatabaseConnector.getAllContacts(DatabaseConnector.java:60) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at com.kt.narendramodi.InfoNarendraModi.onCreate(InfoNarendraModi.java:94) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.Activity.performCreate(Activity.java:5008) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
11-14 12:01:22.495: E/AndroidRuntime(30607): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
11-14 12:01:22.495: E/AndroidRuntime(30607): ... 21 more 
+0

你初始化數據庫變量? –

+0

我沒有得到你......請你詳細說明一下。 – jhon

+1

您需要將數據庫對象初始化爲database = dbHelper.getReadableDatabase() –

回答

3

你必須檢查兩件事情:

  1. 你的數據庫應該被初始化爲讀/寫能力的數據庫。

    database = yourDbHelper.getReadableDatabase();

  2. 您不需要這些參數的選擇,如果你已經完全填補了SQL查詢。

    database.rawQuery("SELECT _id, name FROM tcc_info_table ", null);

+0

尊敬的varevarao「數據庫」是SqliteDataBase類的對象。 – jhon

+2

我沒有看到/爲什麼這意味着你不應該像上面描述的那樣初始化它。 'getReadableDatabase()'返回一個SQLiteDatabase的實例。請參考下面的鏈接,特別是'創建和/或打開數據庫'這一行。http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html#getReadableDatabase() – varevarao