java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法以选择sqlite [英] java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference to select sqlite

查看:117
本文介绍了java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法以选择sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是android world的新手.我有编码问题.这只是一个很小的错误,我什至不知道更改其他方法也不知道它不起作用,但该错误仍然是相同的错误.这里的错误发生在logcat:

I am a newbie of android world. I have a problem of the coding. It was just a tiny error buy i dont know it doesnt work even i change others method but the error still the same error. Here the error occur at logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.example.zellon.surveyapps.DatabaseHelper.getAData()' on a null object reference

我只想选择数据库中的数据以获取ID,但无法发现上述错误.

I just want to select the data in the database to get an id but cant cuz of the error above.

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

I will give a code that i coded for select the data from database

surveyinstruction.java

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

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, null, 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, null, contentValues);

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

因此,在Surveyinstruction.java代码中,我仅从DatabaseHelper.java调用getAData()函数,从数据库中选择一个ID,但错误表明该对象为空对象引用.我不知道错误是什么.我希望有人能帮助我,使其正常运行.谢谢.

So, in the code of surveyinstruction.java i only call getAData() function from DatabaseHelper.java the select an ID from database but the error shown that it null object reference. I dont know what the error is. I hope someone please help me for it to work fine. Thank you.

推荐答案

初始化DatabaseHelper:

Initialize the DatabaseHelper:

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

    DatabaseHelper db = new DatabaseHelper(this);

    //...
}

这篇关于java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法以选择sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆