2

我正在使用 aCursor来获取存储在我的SQLite数据库中的一些数据。当我Cursor有一些来自数据库的数据时,代码可以正常工作。但是当Cursor不包含任何数据时,我得到一个NullPointerExceptionatcursor.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;
    }
4

3 回答 3

6

使用前务必检查getCount

if(mCurSugCnt!=null && mCurSugCnt.getCount()>0 )
{
 mCurSugCnt.moveToFirst();
}
于 2012-12-31T09:42:35.443 回答
1

如果您正在NullPointerException上车,那么您应该在访问之前mCurSugCnt.moveToFirst();设置某种条件来检查是否Cursor mCurSugCntNull 。

喜欢:

private void fillSugCntData(String cnt) {
         mCurSugCnt = mDb.fetchCntNotes(cnt);
         if(mCurSugCnt != null && mCurSugCnt.getCount() > 0)
         {
            mCurSugCnt.moveToFirst();
         }
    }
于 2012-12-31T09:43:39.563 回答
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 "appfinlist" 返回给 mCurSugCnt

return appfinlist;

这就是为什么你得到 Nullpointer 异常,如果你调用查询方法来获取光标,通常它不会返回 null 对象

于 2014-10-16T07:37:11.437 回答