查询中的Android空指针异常游标

时间:2013-12-09 07:05:13

标签: java android

这是我第一次在这里问,通常我通过搜索其他人的帖子得到答案,我有这个代码,但我在这行cur = db.rawQuery(sel, null);空指针选择中得到一个错误。有人可以帮忙吗?

public class DBDataSource {

private SQLiteDatabase db;
private DatabaseHelper dbh;

public DBDataSource(Context ctx) {
    this.dbh = new DatabaseHelper(ctx);
    }

public void open() throws SQLException {
    db = dbh.getWritableDatabase();
}

public void close() {
    dbh.close();
}

public int pencapaian(int p){

String sel = "SELECT price FROM "+DatabaseHelper.TABLE_NAME2
     +" WHERE "+DatabaseHelper.KOLOM_sSCHEDULE+ "= 'Negosiasi'";

Cursor cur ;

cur = db.rawQuery(sel, null);
cur.moveToFirst();

while (!cur.isAfterLast()) {
p=p+cur.getInt(0);          
cur.moveToNext();
    }
cur.close();

return p;
}

我正在另一个班级打电话

public class Menu {
    private DBDataSource dtsrc;
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);      
    pnc=(TextView) findViewById(R.id.txtpencapaian);
            pencapaian=dtsrc.pencapaian(pencapaian);
            pnc.setText(Integer.toString(pencapaian));
}}

日志

12-09 00:36:27.483: E/AndroidRuntime(2329): FATAL EXCEPTION: main
12-09 00:36:27.483: E/AndroidRuntime(2329): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.marketing.hoffmen/com.marketing.hoffmen.Menu}: java.lang.NullPointerException
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.os.Looper.loop(Looper.java:137)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread.main(ActivityThread.java:5103)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at java.lang.reflect.Method.invoke(Method.java:525)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at dalvik.system.NativeStart.main(Native Method)
12-09 00:36:27.483: E/AndroidRuntime(2329): Caused by: java.lang.NullPointerException
12-09 00:36:27.483: E/AndroidRuntime(2329):     at com.marketing.hoffmen.Menu.onCreate(Menu.java:43)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.Activity.performCreate(Activity.java:5133)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-09 00:36:27.483: E/AndroidRuntime(2329):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)

此代码pencapaian=dtsrc.pencapaian(pencapaian);< ==第43行

5 个答案:

答案 0 :(得分:0)

  1. 您的数据库显然没有初始化,这就是您收到该错误的原因

  2. 为了避免将来出现问题,您正在查询单列(Price),但是使用第1列访问它,这是您的另一个问题,只需将cur.getInt(1)替换为cur.getInt(0)`和你的问题已经解决了。

答案 1 :(得分:0)

  

使用此查询我希望它能够正常运行,并确保将活动注册到清单文件

String sel = "SELECT price FROM "+DatabaseHelper.TABLE_NAME2
        +" WHERE "+DatabaseHelper.KOLOM_sSCHEDULE+ =" 'Negosiasi'";

答案 2 :(得分:0)

光标列将从0索引开始计数,因此请尝试访问以下值:

  p=p+cur.getInt(0);     

答案 3 :(得分:0)

打开数据库。

SQLiteDatabase db= SQLiteDatabase.openDatabase(myDatabasePath, null,
                SQLiteDatabase.OPEN_READWRITE);

然后执行查询。

答案 4 :(得分:0)

我认为pencapaian是问题所在。请初始化pencapaian=0;并尝试

pencapaian=dtsrc.pencapaian(pencapaian);

还要确保使用这两行代码,它似乎没有

DBDataSource dtsrc = new DBDataSource(this);
dtsrc.open();