Android-为什么我会得到一个空指针异常(SQLite)
我试图从我的数据库中得到一个平均值。我实例化了一个游标装入器,然后调用一个方法来返回游标。装载背景。根据logcat,这就是我得到nullpointer异常的地方: MyCursor=MyCursorLoader.loadInBackground 这是我创建MyCursorLoader的地方: SQLiteCursorLoaderAverage MyCursorLoader=新SQLiteCursorLoaderAveragegetApplicationContext,databaseHelper,null,null 以下是我的加载背景方法:Android-为什么我会得到一个空指针异常(SQLite),android,sqlite,nullpointerexception,cursor,android-cursorloader,Android,Sqlite,Nullpointerexception,Cursor,Android Cursorloader,我试图从我的数据库中得到一个平均值。我实例化了一个游标装入器,然后调用一个方法来返回游标。装载背景。根据logcat,这就是我得到nullpointer异常的地方: MyCursor=MyCursorLoader.loadInBackground 这是我创建MyCursorLoader的地方: SQLiteCursorLoaderAverage MyCursorLoader=新SQLiteCursorLoaderAveragegetApplicationContext,databaseHelpe
public Cursor loadInBackground() {
Cursor cursor=buildCursor();
if (cursor!=null) {
// Ensure the cursor window is filled
cursor.getCount();
}
return(cursor);
}
以下是我的构建方法:
protected Cursor buildCursor()
{
return(db.getReadableDatabase().rawQuery(rawQuery, null));
}
在rawquery为的情况下,从random中选择avgnumbers
然后我通过这样做得到平均值:MyCursor.getDouble0
我的日志:
03-19 22:19:28.013: E/AndroidRuntime(1226): Caused by: java.lang.NullPointerException
03-19 22:19:28.013: E/AndroidRuntime(1226): tcom.example.helloworld.SQLiteCursorLoaderAverage.buildCursor(SQLiteCursorLoaderAverage.java:52)
03-19 22:19:28.013: E/AndroidRuntime(1226): at com.example.helloworld.AbstractCursorLoader.loadInBackground(AbstractCursorLoader.java:21)
03-19 22:19:28.013: E/AndroidRuntime(1226): at com.example.helloworld.AverageActivity.onCreate(AverageActivity.java:27)
我不知道为什么会出现null指针异常。
非常感谢
编辑:
这里是设置我的数据库的地方,它以null开头
public class SQLiteCursorLoaderAverage extends AbstractCursorLoader
{
SQLiteOpenHelper db=null;
String rawQuery=null;
String[] args=null;
Context context;
/**
* Creates a fully-specified SQLiteCursorLoader. See
* {@link SQLiteDatabase#rawQuery(SQLiteDatabase, String, String[])
* SQLiteDatabase.rawQuery()} for documentation on the
* meaning of the parameters. These will be passed as-is
* to that call.
*/
public SQLiteCursorLoaderAverage(Context newContext, SQLiteOpenHelper db, Bundle searchParameters, String[] args)
{
super(newContext);
this.db=db;
this.rawQuery=createQuery(searchParameters);
this.args=args;
context = newContext;
}
…您的数据库很可能是空的,也许您可以调试以找出它的值。要么db是空的,要么db.getReadableDatabase返回空的。根据给出的代码,我无法判断发生错误的方法是哪个.Add SQLiteCursorLoaderAverage.buildCursor。