从数据库中检索数据时显示NUll POINTER EXCEPTION

时间:2012-11-14 09:54:29

标签: android database

既然,我是android世界的新手,请帮助我。当我使用Android SQLite通过这些代码从数据库中检索数据时 -

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()声明为 -

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

“name”只是我的App中数据库的列名。

当我运行我的App时,它在getAllContact()方法中显示NULL POINTER Exception。即使我通过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

1 个答案:

答案 0 :(得分:3)

你必须检查两件事:

  1. 您的数据库应初始化为可读/可写数据库。

    database = yourDbHelper.getReadableDatabase();

  2. 如果您已完全填写SQL查询,则不需要这些选择参数。

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