I got a problem on my code, it says
07-22 21:40:43.059 28529-28529/plp.plpedia.developer.larriane.my_plpedia E/AndroidRuntime: FATAL EXCEPTION: main
Process: plp.plpedia.developer.larriane.my_plpedia, PID: 28529 java.lang.RuntimeException: Unable to destroy activity {plp.plpedia.developer.larriane.my_plpedia/plp.plpedia.developer.larriane.my_plpedia.MainNotes}: java.lang.NullPointerException: Attempt to invoke virtual method 'void plp.plpedia.developer.larriane.my_plpedia.NotesCRUDManager.closeDatabase()' on a null object reference at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:4874) at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:4892) at android.app.ActivityThread.access$1600(ActivityThread.java:218) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1772) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6917) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void plp.plpedia.developer.larriane.my_plpedia.NotesCRUDManager.closeDatabase()' on a null object reference at plp.plpedia.developer.larriane.my_plpedia.MainNotes.onDestroy(MainNotes.java:60)
This is my NotesCRUDManager.java, database.
public class NotesCRUDManager {
private SQLiteDatabase db;
private Context context;
private String DBName = "NotesDB";
private String TableName = "mynotes";
private List<Integer> ids_list = new ArrayList<Integer>();
private List<String> titles_list = new ArrayList<String>();
private List<String> notes_list = new ArrayList<String>();
private List<String> remark_list = new ArrayList<String>();
NotesCRUDManager(Context context) {
this.context = context;
// As soon as we call this class our database should be created or opened
AccessDatabase();
AccessNotesTable();
}
// Opening database
public void AccessDatabase() {
// create database named as "Catalog"
// context.MODE_PRIVATE indicates this database can only be access through this application
db = context.openOrCreateDatabase(DBName, context.MODE_PRIVATE, null);
}
// Selecting our table
public void AccessNotesTable() {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TableName + "(_id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT, note TEXT, remark TEXT)");
}
// Inserting product into table
public void InsertNote(String title, String note, String remark) {
ContentValues values = new ContentValues(); // create an instance of ContentValues
values.put("title", title); // parameters are (key, value) but the key = columnName in our Database Table
values.put("note", note);
values.put("remark", remark);
//db.insert() is similar to prepared statement in Java. It will prevent SQL Injections
db.insert(TableName, null, values);
}
public void EditNote(int id, String title, String note, String remark) {
ContentValues values = new ContentValues(); // create an instance of ContentValues
values.put("title", title); // parameters are (key, value) but the key = columnName in our Database Table
values.put("note", note);
values.put("remark", remark);
//db.update removes the risk of SQL Injection
db.update(TableName, values, "_id = ?", new String[]{"" + id});
}
public void DeleteNote(int id) {
// simple sql statement
db.execSQL("DELETE FROM " + TableName + " WHERE _id = " + id + ";");
}
// Showing all rows from table
public void showNote(List<Integer> ids_list, List<String> titles_list, List<String> notes_list, List<String> remark_list) {
// this will execute select query
Cursor cursor = db.rawQuery("SELECT * FROM " + TableName + " ORDER BY _id DESC", null);
// getting data from all rows
while (cursor.moveToNext()) {
//filling up the arraylist with database columns
ids_list.add(cursor.getInt(0));
titles_list.add(cursor.getString(1));
notes_list.add(cursor.getString(2));
remark_list.add(cursor.getString(3));
}
}
// Relesing database object
public void closeDatabase() {
// close the database, always remember only close the database when it is opened
db.close();
}
}
This is my MainNotes.java, noteslist seems like the error for me. But I don't know how to change my code. .
public class MainNotes extends AppCompatActivity implements View.OnClickListener {
private NotesCRUDManager nmanager;
private Button create, clear, noteslist;
private EditText title, note, remark;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_notes);
create = (Button)findViewById(R.id.create);
clear = (Button)findViewById(R.id.clear);
noteslist = (Button)findViewById(R.id.create_shownotes);
title = (EditText)findViewById(R.id.create_title_edittext);
note = (EditText)findViewById(R.id.create_note_edittext);
remark = (EditText)findViewById(R.id.create_remark_edittext);
create.setOnClickListener(this);
clear.setOnClickListener(this);
noteslist.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v==create) {
nmanager = new NotesCRUDManager(this);
nmanager.InsertNote(title.getText().toString(), note.getText().toString(), remark.getText().toString());
Toast.makeText(this, "Note Created", Toast.LENGTH_SHORT).show();
}
else if (v==clear){
// clear all text inside Edittext
title.setText("");
note.setText("");
remark.setText("");
}
else if (v==noteslist) {
// start NotesList Activity
Intent intent = new Intent(this, ShowNotesList.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
}
}
@Override
protected void onDestroy() {
nmanager.closeDatabase();
super.onDestroy();
}
}