我在 SQLite 中执行 COUNT() 时收到 NullPointerException。请参阅以下代码 -
public int getrcofpersons() {
// TODO Auto-generated method stub
SQLiteDatabase myDB;
int values = 0;
int count = 0;
try {
myDB=this.openDataBase();
Cursor c=myDB.rawQuery("select count(PersonID) from Persons;",null);
if (c != null ) {
String h = "";
c.moveToFirst();
count = c.getInt(c.getColumnIndex("PersonID"));
// put your code to get data from cursor
}
if(c != null) {
c.close();
myDB.close();
}
} catch(SQLException sqle) {
throw sqle;
}
System.out.println(count + " is the rowcount of persons.");
return count;
}
此函数返回 Null 值。甚至System.out.println(count + " is the rowcount of persons.");
还将count
值显示为 0,即初始化值。现在我无法发布 logcat,因为此代码片段与您可能不理解的许多其他功能相关联。所以,请告诉我我是否在这段代码中犯了任何错误。
请看下面的代码。此代码正在调用上述方法(在 helper.java 中)。适配器.java:
public int getrowcountofpersons() {
// TODO Auto-generated method stub
int rc = 0;
try {
//open();
rc = mDbHelper.getrcofpersons(); //Here the nullPointerException is raised.
// close();
}
catch (Exception e)
{Log.v("getrowcountofpersons()","5");
Log.v("Error in getrowcountofpersons() of adapter : ",e.toString());
}
Log.v("getrowcountofpersons()","6");
System.out.println(rc + " is the rowcount of persons in adapter.");
return rc;
}
提前致谢。