1

搜索了一大堆相同的问题,我不知道为什么我会得到 NPE。请帮忙。

我从表中读取数据的代码是,

private String []cols = {"name","id"};

public void seeRecord(String a, int b){



    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
    String nameintable = c.getString(c.getColumnIndex(cols[0]));

    int idintable = c.getInt(c.getColumnIndex(cols[1]));

    a=nameintable;
    b=idintable;

}
    c.close();
}

向表中插入数据是:

public void addRecord(String name, int id){
    db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put("name",name);
    values.put("id", id);

    db.insert("vijay", null, values);
    db.close();
    }

日志错误:

07-30 12:11:34.755: E/AndroidRuntime(15894): FATAL EXCEPTION: main
07-30 12:11:34.755: E/AndroidRuntime(15894): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$1.onClick(View.java:2072)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View.performClick(View.java:2408)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.os.Looper.loop(Looper.java:123)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:11:34.755: E/AndroidRuntime(15894):    at android.view.View$1.onClick(View.java:2067)
07-30 12:11:34.755: E/AndroidRuntime(15894):    ... 11 more
07-30 12:11:34.755: E/AndroidRuntime(15894): Caused by: java.lang.NullPointerException
07-30 12:11:34.755: E/AndroidRuntime(15894):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:11:34.755: E/AndroidRuntime(15894):    ... 15 more
07-30 12:23:40.606: E/AndroidRuntime(18817): FATAL EXCEPTION: main
07-30 12:23:40.606: E/AndroidRuntime(18817): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$1.onClick(View.java:2072)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View.performClick(View.java:2408)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.os.Looper.loop(Looper.java:123)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:40.606: E/AndroidRuntime(18817):    at android.view.View$1.onClick(View.java:2067)
07-30 12:23:40.606: E/AndroidRuntime(18817):    ... 11 more
07-30 12:23:40.606: E/AndroidRuntime(18817): Caused by: java.lang.NullPointerException
07-30 12:23:40.606: E/AndroidRuntime(18817):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:23:40.606: E/AndroidRuntime(18817):    ... 15 more
07-30 12:23:49.245: E/AndroidRuntime(18861): FATAL EXCEPTION: main
07-30 12:23:49.245: E/AndroidRuntime(18861): java.lang.IllegalStateException: Could not execute method of the activity
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$1.onClick(View.java:2072)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View.performClick(View.java:2408)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$PerformClick.run(View.java:8816)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Handler.handleCallback(Handler.java:587)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.os.Looper.loop(Looper.java:123)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at dalvik.system.NativeStart.main(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861): Caused by: java.lang.reflect.InvocationTargetException
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.example.databaseaccess.MainActivity.see(MainActivity.java:42)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invokeNative(Native Method)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at java.lang.reflect.Method.invoke(Method.java:521)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.view.View$1.onClick(View.java:2067)
07-30 12:23:49.245: E/AndroidRuntime(18861):    ... 11 more
07-30 12:23:49.245: E/AndroidRuntime(18861): Caused by: java.lang.IllegalStateException: database not open
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1333)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1315)
07-30 12:23:49.245: E/AndroidRuntime(18861):    at com.example.databaseaccess.DbHelper.seeRecord(DbHelper.java:61)
07-30 12:23:49.245: E/AndroidRuntime(18861):    ... 15 more

两者都在按钮的 onclick 事件中调用。插入工作正常。但是当点击阅读按钮时,应用程序崩溃。错误在哪里?

4

5 回答 5

3

尝试这个

public void seeRecord(String a, int b){

    db = this.getReadableDatabase();

    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
        String nameintable = c.getString(c.getColumnIndex(cols[0]));

        int idintable = c.getInt(c.getColumnIndex(cols[1]));

        a=nameintable;
        b=idintable;

    }
    c.close();
}
于 2013-07-30T06:54:23.020 回答
3

我总是这样使用:

public List<Ticket> getTickets() {
        openDataBase();
        Cursor cursor = myDataBase.rawQuery("Select * from ABC" , null);
        while (cursor.moveToNext()) {
            //parse data here
        }
        cursor.close();
        closeDatabase();
        return tickets;
    }

如果上述代码不能解决您的问题,请尝试以下操作:

public List<Ticket> getTickets() {
    openDataBase();
    Cursor cursor = myDataBase.rawQuery("Select * from ABC" , null);
    while (!cursor.isAfterLast()) {
        cursor.moveToNext();
        //parse data here
    }
    cursor.close();
    closeDatabase();
    return tickets;
}
于 2013-07-30T07:25:43.500 回答
1

您可能希望发布 logcat 以使此调试更容易。但是基于该代码,我假设您的数据库为空。

在您的插入中,您调用这两种方法。

db = this.getWritableDatabase();

db.close();

我假设它在这条线上崩溃了。

Cursor c = db.rawQuery("select * from vijay", null);

这意味着您将需要像在插入中那样重新打开/关闭数据库。

于 2013-07-30T06:55:00.540 回答
1

尝试

public void seeRecord(String a, int b){
    db = this.getWritableDatabase();
    Cursor c = db.rawQuery("select * from vijay", null);
    c.moveToFirst();
    while(c.moveToNext()){
    String nameintable = c.getString(c.getColumnIndex(cols[0]));

    int idintable = c.getInt(c.getColumnIndex(cols[1]));

    a=nameintable;
    b=idintable;
   }
    c.close();
    db.close();
}
于 2013-07-30T06:52:35.840 回答
1

用这个替换你的代码

public Cursor seeRecord()
{
    db.open(); 
    Cursor c = db.rawQuery("select * from vijay", null);
    db.close();

    return c;
}

在要更新视图的方法上编写以下代码

db.open(); 
Cursor c = seeRecord();

 c.moveToFirst();

while(!c.isAfterLast())
{
    if(c != null)
    {
        String nameintable = c.getString(0);
        int idintable = c.getInt(1);

        Your_View.id.setText(nameintable);
        Your_View.id.setText(idintable);
    }
    c.moveToNext();
}
c.close();  
db.close();
于 2013-07-30T08:42:13.423 回答