光标movetofirst()&空指针异常

时间:2012-12-31 09:40:48

标签: android android-cursor

我使用Cursor来获取存储在SQLite数据库中的一些数据。当我的Cursor包含来自数据库的一些数据时,代码工作正常。但是当Cursor不包含任何数据时,我会在NullPointerException方法获得cursor.movetofirst()。我在其他地方使用了类似的代码,但movetofirst()方法在其他地方没有给出NullPointerException。我想这几天,我无法弄清问题是什么。请帮助。

    public class Country extends Activity {

        private DbAdapter mDb;
        private Cursor mCurSugCnt;
        String country=null;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.country);

            mDb=new DbAdapter(this);
            mDb.open();

            country= this.getIntent().getExtras().getString("country");
            fillSugCntData(country);

        }

    private void fillSugCntData(String cnt) {
        //I have tried multiple methods. The problem occurs when there is no data returned by any of the methods called
        //mCurSugCnt=mDb.fetchCatNotes("abc");
        mCurSugCnt=mDb.fetchCntNotes(cnt);

        //This is where I called the null pointer exception in case no data is returned by the cursor
        mCurSugCnt.moveToFirst();

    }


---------------------

public Cursor fetchCntNotes(String cnt){

        int id;
        Cursor appfinlist=null;
        Cursor temp=mDb.query(CNT_TABLE, new String[]{KEY_ROWID}, KEY_CNTNM + "=\"" + cnt + "\"", null, null , null, null);
        Cursor applist;

        if(temp.moveToFirst()){
            id=temp.getInt(temp.getColumnIndexOrThrow(DbAdapter.KEY_ROWID));

            applist=mDb.rawQuery("SELECT appcntid FROM appcntlist WHERE cntappid = "+id, null);
            if(applist.moveToFirst()){
                String[] cntin=new String[applist.getCount()];
                for(int i=0;i<applist.getCount();i++){
                    cntin[i]=""+applist.getInt(applist.getColumnIndexOrThrow(DbAdapter.KEY_APPCNTID));
                    Log.d("amit","countryname id cnt var: " + cntin[i]);
                    applist.moveToNext();   
                }

                String query = "SELECT * FROM applist"
                    + " WHERE _id IN (" + makePlaceholders(applist.getCount()) + ")";

                appfinlist = mDb.rawQuery(query, cntin);
                Log.d("amit","countryname id get count: " + appfinlist.getCount());
            }
        }    
            return appfinlist;
    }

    private String makePlaceholders(int count) {
        String toRet="?";

        for (int i=1;i<count;i++){
            toRet=toRet + ",?";
        }
        return toRet;
    }

3 个答案:

答案 0 :(得分:4)

使用前请务必核对getCount

if(mCurSugCnt!=null && mCurSugCnt.getCount()>0 )
{
 mCurSugCnt.moveToFirst();
}

答案 1 :(得分:1)

如果您在NullPointerException上收到mCurSugCnt.moveToFirst();,那么在访问之前,您应该设置某种条件来检查Cursor mCurSugCnt是否为空。< / p>

像:

private void fillSugCntData(String cnt) {
         mCurSugCnt = mDb.fetchCntNotes(cnt);
         if(mCurSugCnt != null && mCurSugCnt.getCount() > 0)
         {
            mCurSugCnt.moveToFirst();
         }
    }

答案 2 :(得分:0)

检查您的代码

Cursor appfinlist=null; 
Cursor temp=mDb.query(CNT_TABLE, new String[]{KEY_ROWID}, KEY_CNTNM + "=\"" + cnt + "\"", null, null , null, null);

如果没有找到记录,则在

块之后
if(temp.moveToFirst()){
    ....
}

你返回null&#34; appfinlist&#34;到mCurSugCnt

return appfinlist;

这就是你得到Nullpointer异常的原因,如果你调用查询方法来获取游标,通常它不会返回null对象