I'm trying to get a simple note-taking app to work, but I keep on getting a nullPointerException when calling a certain method.
The value from the textEdit in the dialog layout is retrieved just fine. I checked by logging the results. The problem occurs when I try to run the createNotes(content) method.
public class Dialog extends Activity {
NotesBean notesBean = null;
public NotesDao datasource;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("log", "Running NotiMgr");
setContentView(R.layout.dialog);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.dialog_background);
layout.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText mEdit = (EditText) v.findViewById(R.id.editText);
String content = mEdit.getText().toString();
Log.v("EditText", content);
//THIS IS WHERE THE ERROR OCCURS
datasource.createNotes(content);
finish();
}
});
}
This class deals with inserts, deletes and db related stuff. I've tried debugging, but it never reaches this class.
public class NotesDao {
public NotesBean createNotes(String content) {
try {
Log.v("createNotes", content);
ContentValues values = new ContentValues();
values.put(DbHelper.COLUMN_CONTENT, content);
long insertSeq = database.insert(DbHelper.TABLE_NOTES, null, values);
Cursor cursor = database.query(
DbHelper.TABLE_NOTES, //String table
allColumns, //String[] columns
DbHelper.COLUMN_SEQ + " = " + insertSeq, //String selection
null, //String[] selectionArgs
null, //String groupBy
null, //String having
null); //String orderBy
cursor.moveToFirst();
NotesBean newNotes = cursorToNotes(cursor);
cursor.close();
return newNotes;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
btw I have also confirmed that the db is opened through logcat... But it was in a different activity. Does that mean I should close it and open it again in this activity?
Any help would be greatly appreciated! Thanks!
change:
public NotesDao datasource;
to
public NotesDao datasource=new NotesDao();
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments