0

嗨,我是 android 编程新手,我不知道或者我真的无法弄清楚为什么在我单击我创建的应用程序上的按钮后会收到此错误消息

java.lang.NullPointerException at com.example.dailydoseofhappiness.MainActivity.searchRecord(MainActivity.java:62) at com.example.dailydoseofhappiness.MainActivity.searchRecord(MainActivity.java:52)

这是代码片段。

public void onClick(View arg){ 
     if(arg.getId()==R.id.btnfortune){ //this is line 52
         searchRecord(count);
     }

 }

public void searchRecord(int count)
throws SQLException
{
    Cursor rsCursor;
    String [] rsFields = {"mesNum","Message"}; // this is line 62
    rsCursor = dbm.dbase.query("MessageFile", rsFields, "mesNum = " + count, null,       null, null, null, null);
    rsCursor.moveToFirst();

    if(rsCursor.isAfterLast()==false){

     lblmessageS.setText(rsCursor.getString(0));
    }
    rsCursor.close();
     }

如果有什么问题,谁能检查这个代码片段非常感谢

4

1 回答 1

0

试试这个searchRecord方法。我添加了一些愚蠢的检查,所以如果任何变量为空,它会警告你:

public void searchRecord(int count) throws SQLException {
    Cursor rsCursor;
    String [] rsFields = {"mesNum","Message"}; // this is line 62

    if (dbm == null)
        throw new Exception("dbm object is null");

    if (dbm.dbase == null)
        throw new Exception("dbm.dbase is null");

    rsCursor = dbm.dbase.query("MessageFile", rsFields, "mesNum = " + count, null, null, null, null, null);

    if (rsCursor == null)
        throw new Exception("rsCursor is null");

    rsCursor.moveToFirst();

    if (rsCursor.isAfterLast()==false){
        if (lblmessageS == null)
            throw new Exception("lblmessageS is null");

        lblmessageS.setText(rsCursor.getString(0));
    }

    rsCursor.close();
 }
于 2013-03-07T15:30:56.520 回答