java.lang.NullPointerException:尝试在空对象引用上调用虚方法以选择sqlite

前端之家收集整理的这篇文章主要介绍了java.lang.NullPointerException:尝试在空对象引用上调用虚方法以选择sqlite前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是 android世界的新手.我有编码问题.这只是一个小错误购买我不知道它不工作,即使我改变其他方法,但错误仍然是相同的错误.这里错误发生在logcat:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.zellon.surveyapps.DatabaseHelper.getAData()' on a null object reference

我只想选择数据库中的数据来获取id,但不能忽略上面的错误.

我将给出一个代码,用于从数据库中选择数据

surveyinstruction.java

package com.example.zellon.surveyapps;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class surveyinstruction extends AppCompatActivity {
DatabaseHelper myDb;

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surveyinstruction);

    TextView user = (TextView) findViewById(R.id.user);

    Intent in = getIntent();

    String nameUser = in.getStringExtra("nameUser");
    String tarikhUser = in.getStringExtra("tarikhUser");

    user.setText("Selamat Datang " + nameUser);

    Button btnSeterusnya = (Button) findViewById(R.id.btnTerus);

    btnSeterusnya.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intentTerus = new Intent(getApplicationContext(),surveymain.class);
            Cursor res = myDb.getAData();
            intentTerus.putExtra("id",res.getString(0));
            Log.e("ID ",res.getString(0));
            startActivity(intentTerus);
        }
    });
}
}

这是数据库处理程序

DatabaseHelper.java

package com.example.zellon.surveyapps;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.sqliteDatabase;
import android.database.sqlite.sqliteOpenHelper;
public class DatabaseHelper extends sqliteOpenHelper {
public static final String DATABASE_NAME = "survey.db";
public static final String TABLE_USER = "user";
public static final String COL_USER_1 = "ID";
public static final String COL_USER_2 = "NAMA";
public static final String COL_USER_3 = "TARIKH";
public static final String COL_USER_4 = "MARKAH";

public static final String TABLE_QUESTION = "question";

public static final String COL_QUES_1 = "ID";
public static final String COL_QUES_2 = "IDUSER";
public static final String COL_QUES_3 = "K1";
public static final String COL_QUES_4 = "K2";
public static final String COL_QUES_5 = "K3";
public static final String COL_QUES_6 = "K4";
public static final String COL_QUES_7 = "K5";
public static final String COL_QUES_8 = "A1";
public static final String COL_QUES_9 = "A2";
public static final String COL_QUES_10 = "A3";
public static final String COL_QUES_11 = "A4";
public static final String COL_QUES_12 = "A5";
public static final String COL_QUES_13 = "V1";
public static final String COL_QUES_14 = "V2";
public static final String COL_QUES_15 = "V3";
public static final String COL_QUES_16 = "V4";
public static final String COL_QUES_17 = "V5";
public static final String COL_QUES_18 = "D1";
public static final String COL_QUES_19 = "D2";
public static final String COL_QUES_20 = "D3";
public static final String COL_QUES_21 = "D4";
public static final String COL_QUES_22 = "D5";
public static final String COL_QUES_23 = "TOTK";
public static final String COL_QUES_24 = "TOTA";
public static final String COL_QUES_25 = "TOTV";
public static final String COL_QUES_26 = "TOTD";

public DatabaseHelper(Context context) {
    super(context,DATABASE_NAME,null,1);
}

@Override
public void onCreate(sqliteDatabase db) {
    db.execsql("create table " + TABLE_USER + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAMA TEXT,TARIKH TEXT,MARKAH INTEGER)");
    db.execsql("create table " + TABLE_QUESTION + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,IDUSER INTEGER,K1 TEXT,K2 TEXT,K3 TEXT,K4 TEXT,K5 TEXT,A1 TEXT,A2 TEXT,A3 TEXT,A4 TEXT,A5 TEXT,V1 TEXT,V2 TEXT,V3 TEXT,V4 TEXT,V5 TEXT,D1 TEXT,D2 TEXT,D3 TEXT,D4 TEXT,D5 TEXT,TOTK INTEGER,TOTA INTEGER,TOTV INTEGER,TOTD INTEGER)");
}

@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
    db.execsql("DROP TABLE IF EXISTS " + TABLE_USER);
    db.execsql("DROP TABLE IF EXISTS " + TABLE_QUESTION);
    onCreate(db);
}

public boolean insertDataUser(String nama,String tarikh){
    sqliteDatabase db = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_USER_2,nama);
    contentValues.put(COL_USER_3,tarikh);
    long result = db.insert(TABLE_USER,contentValues);

    if(result == -1)
        return false;
    else
        return true;
}

public Cursor getCertainData(String namaUser,String tarikhUser){
    String selectQuery = "select * from " + TABLE_USER + " where NAMA like '" + namaUser + "' AND TARIKH like '" + tarikhUser + "'";
    sqliteDatabase db = this.getWritableDatabase();
    Cursor res = db.rawQuery(selectQuery,null);
    return res;
}

public Cursor getAData(){
    sqliteDatabase db = this.getWritableDatabase();
    String selectAQuery = "SELECT ID FROM " + TABLE_USER;
    Cursor re = db.rawQuery(selectAQuery,null);
    return re;
}

public boolean insertDataQues(int id,String k1,String k2,String k3,String k4,String k5,String a1,String a2,String a3,String a4,String a5,String v1,String v2,String v3,String v4,String v5,String d1,String d2,String d3,String d4,String d5,int totalk,int totala,int totalv,int totald){
    sqliteDatabase dbQues = this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(COL_QUES_2,id);
    contentValues.put(COL_QUES_3,k1);
    contentValues.put(COL_QUES_4,k2);
    contentValues.put(COL_QUES_5,k3);
    contentValues.put(COL_QUES_6,k4);
    contentValues.put(COL_QUES_7,k5);

    contentValues.put(COL_QUES_8,a1);
    contentValues.put(COL_QUES_9,a2);
    contentValues.put(COL_QUES_10,a3);
    contentValues.put(COL_QUES_11,a4);
    contentValues.put(COL_QUES_12,a5);

    contentValues.put(COL_QUES_13,v1);
    contentValues.put(COL_QUES_14,v2);
    contentValues.put(COL_QUES_15,v3);
    contentValues.put(COL_QUES_16,v4);
    contentValues.put(COL_QUES_17,v5);

    contentValues.put(COL_QUES_18,d1);
    contentValues.put(COL_QUES_19,d2);
    contentValues.put(COL_QUES_20,d3);
    contentValues.put(COL_QUES_21,d4);
    contentValues.put(COL_QUES_22,d5);

    contentValues.put(COL_QUES_23,totalk);
    contentValues.put(COL_QUES_24,totala);
    contentValues.put(COL_QUES_25,totalv);
    contentValues.put(COL_QUES_26,totald);

    long result = dbQues.insert(TABLE_QUESTION,contentValues);

    if(result == -1)
        return false;
    else
        return true;
}
}

因此,在surveyinstruction.java的代码中,我只从DatabaseHelper.java调用getAData()函数,从数据库中选择一个ID,但错误显示为null对象引用.我不知道错误是什么.我希望有人能帮助我,让它工作正常.谢谢.

解决方法

初始化DatabaseHelper:
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.surveyinstruction);

    DatabaseHelper db = new DatabaseHelper(this);

    //...
}
原文链接:https://www.f2er.com/java/127588.html

猜你在找的Java相关文章