Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.NullPointerException: Attempt to invoke virtual method on a null object reference to select sqlite

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

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

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);
        }
    });
}
}

And here is for database handler

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;
}
}

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.

like image 694
Zulhisyam Avatar asked Nov 10 '15 10:11

Zulhisyam


1 Answers

Initialize the DatabaseHelper:

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

    DatabaseHelper db = new DatabaseHelper(this);

    //...
}
like image 76
antonio Avatar answered Nov 15 '22 10:11

antonio