Zulhisyam
Zulhisyam

Reputation: 69

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.

Upvotes: 5

Views: 48076

Answers (4)

Aung Phyo Zaw
Aung Phyo Zaw

Reputation: 17

What about this , I had declared the Helper class, " NoteDB db = new NoteDB(this)" But the bug error indicate null pointer exception of this , " String title = db.getTitle" , no useful the declaration of the line " NoteDB db = new NoteDB(this)".

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_note_detail);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Intent i = getIntent();
    long id = i.getLongExtra("ID",1);
    NoteDatabase db = new NoteDatabase(getApplicationContext());
    Note note = db.getNote(id);
    getSupportActionBar().setTitle(note.getTitle());
    Toast.makeText(this,"ID -> "+id,Toast.LENGTH_LONG).show();
  //  Toast.makeText(this,"Title -> "+note.getTitle(),Toast.LENGTH_LONG).show();
    FloatingActionButton fab = findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
}

Upvotes: 0

Shadow
Shadow

Reputation: 33

I had a similar problem with getId from the database. I found that I was not taking the database from mainAcitivity.java.

    public adapter(Context mCtx, int listLayoutRes, List<employee> employeeList,SQLiteDatabase mDatabase) {
    super(mCtx, listLayoutRes, employeeList);

    this.mCtx = mCtx;
    this.listLayoutRes = listLayoutRes;
    this.employeeList = employeeList;
    this.mDatabase = mDatabase;
}

and using this constructor from mainAcitivity

    adpt= new adapter(this,R.layout.row,employeeList,mdatabase);

solved my problem. I hope this helps.

Upvotes: 0

antonio
antonio

Reputation: 18242

Initialize the DatabaseHelper:

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

    DatabaseHelper db = new DatabaseHelper(this);

    //...
}

Upvotes: 14

Knu8
Knu8

Reputation: 478

You have not initialized your reference to DatabaseHelper myDb;. Thats why the nullpointer exception.

Upvotes: 5

Related Questions