Reputation: 1119
I am using a Cursor
to fetch some data stored in my SQLite
database. The code works fine when my Cursor
has some data from the database. But when the Cursor
does not contain any data, I get a NullPointerException
at cursor.movetofirst()
method. I have used a similar code elsewhere but movetofirst()
method does not give a NullPointerException
elsewhere. I am trying this since a couple of days and I'm not able to figure out what is the problem. Kindly help.
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;
}
Upvotes: 2
Views: 6208
Reputation: 371
check your code
Cursor appfinlist=null;
Cursor temp=mDb.query(CNT_TABLE, new String[]{KEY_ROWID}, KEY_CNTNM + "=\"" + cnt + "\"", null, null , null, null);
and if there is no record find, after your block of
if(temp.moveToFirst()){
....
}
you return null "appfinlist" to mCurSugCnt
return appfinlist;
that is why you got Nullpointer exception, normally it won't return null object if you call query method to get cursor
Upvotes: 0
Reputation: 128428
If you are getting NullPointerException
on mCurSugCnt.moveToFirst();
then you should put some kind of condition to check whether Cursor mCurSugCnt
is Null or not before accessing it.
like:
private void fillSugCntData(String cnt) {
mCurSugCnt = mDb.fetchCntNotes(cnt);
if(mCurSugCnt != null && mCurSugCnt.getCount() > 0)
{
mCurSugCnt.moveToFirst();
}
}
Upvotes: 1
Reputation: 33248
Always check getCount before use it.
if(mCurSugCnt!=null && mCurSugCnt.getCount()>0 )
{
mCurSugCnt.moveToFirst();
}
Upvotes: 6