如何解决这个java.lang.NullPointerException

时间:2013-03-07 15:08:13

标签: sqlite

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

  

java.lang.NullPointerException at   com.example.dailydoseofhappiness.MainActivity.searchRecord(MainActivity.java:62)   在   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();
     }

任何人都可以检查这个代码片段,如果有什么不对,非常感谢

1 个答案:

答案 0 :(得分:0)

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

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();
 }