Rishabh Rishabh - 4 months ago 13
Java Question

How to view all data from database?

This is my Database helper class.

package com.dutt.rishabh.assignmentbuddy;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
* Created by Rishabh on 09-07-2016.
*/

public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME="assignment.db";
public static final String TABLE_NAME="assignment_data";
public static final int DATABASE_VERSION=1;
public static final String COL_1="ID";
public static final String COL_2="ASSIGNMENT_NAME";
public static final String COL_3="SUBMISSION_DATE";
public static final String COL_4="INFORMATION";
public static final String COL_5="REMARKS";

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

@Override
public void onCreate(SQLiteDatabase db) {
String create="create table "+TABLE_NAME+"("+COL_1+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COL_2+" TEXT NOT NULL,"+COL_3+" TEXT NOT NULL,"+COL_4+" TEXT NOT NULL,"+COL_5+" TEXT NOT NULL"+")";
db.execSQL(create);

}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
String upgrade="Drop table if exists "+TABLE_NAME;
db.execSQL(upgrade);
onCreate(db);

}
public boolean addAssignment(Assignment x){
SQLiteDatabase myDB=this.getWritableDatabase();
ContentValues content=new ContentValues();
content.put(COL_2,x.getName());
content.put(COL_3,x.getDate());
content.put(COL_4,x.getInfo());
content.put(COL_5,x.getRemarks());
long result=myDB.insert(TABLE_NAME,null,content);
if(result == -1){
return false;
}
else{
return true;
}
}
public Cursor viewAll(){
SQLiteDatabase MyDB=this.getReadableDatabase();
Cursor res=myDB.rawQuery("SELECT * FROM "+DatabaseHelper.TABLE_NAME,null);
return res;
}
/*public Cursor view(){
Cursor cursor=myDB.rawQuery("SELECT * FROM "+TABLE_NAME+" WHERE ID=?",new String[]{COL_1+""});
return cursor;
}*/
}


This is my View class

package com.dutt.rishabh.assignmentbuddy;

import android.app.Activity;

import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
* Created by Rishabh on 09-07-2016.
*/

public class View extends Activity {
Button viewAll,show;
EditText id;
DatabaseHelper dbHelper;
int id1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
viewAll=(Button)findViewById(R.id.viewAll);
show=(Button)findViewById(R.id.show);
id=(EditText)findViewById(R.id.id);
loadData();
//showData();

}
public void loadData(){
viewAll.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(android.view.View view) {
Cursor cursor = dbHelper.viewAll();
if(cursor.getCount() == 0){
Toast.makeText(getApplicationContext(),"No Data for any Assignment found!",Toast.LENGTH_SHORT).show();
}
StringBuffer s = new StringBuffer("");
while(cursor.moveToNext()){
s.append("ID:"+cursor.getString(0)+"\n");
s.append("ASSIGNMENT NAME:"+cursor.getString(1)+"\n");
s.append("SUBMISSION DATE:"+cursor.getString(2)+"\n");
s.append("INFORMATION:"+cursor.getString(3)+"\n");
s.append("REMARKS:"+cursor.getString(4)+"\n\n");
}
showMessage("ASSIGNMENT DETAILS:",s.toString());
}
});


}
/*public void showData(){
show.setOnClickListener(new android.view.View.OnClickListener() {
@Override
public void onClick(android.view.View view) {
id1=Integer.parseInt(id.getText().toString());
Cursor m=dbHelper.view(id1);
if(m.getCount()==0){
Toast.makeText(View.this,"No Data for the Assignment found!",Toast.LENGTH_SHORT).show();
}
StringBuffer sb= new StringBuffer();
while(m.moveToNext()){
sb.append("ID:"+m.getString(0)+"\n");
sb.append("ASSIGNMENT NAME:"+m.getString(1)+"\n");
sb.append("SUBMISSION DATE:"+m.getString(2)+"\n");
sb.append("INFORMATION:"+m.getString(3)+"\n");
sb.append("REMARKS:"+m.getString(4)+"\n\n");
}
showMessage("ASSIGNMENT DETAILS:",sb.toString());
}
});

}*/
public void showMessage(String title,String mess){
AlertDialog.Builder x=new AlertDialog.Builder(View.this);
x.setCancelable(true);
x.setTitle(title);
x.setMessage(mess);
x.show();
}
}


And This is the error I get

07-10 08:51:45.497 27027-27027/com.dutt.rishabh.assignmentbuddy E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.dutt.rishabh.assignmentbuddy, PID: 27027
java.lang.NullPointerException
at com.dutt.rishabh.assignmentbuddy.View$1.onClick(View.java:36)
at android.view.View.performClick(View.java:4482)
at android.view.View$PerformClick.run(View.java:18787)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)

Answer

I don't see your DatabaseHelper instance (dbHelper) getting initialized anywhere.

You need to initialize dbHelper before fetching all the records something like this:

dbHelper = new DatabaseHelper(this);