You have to re-open the database in the readNote()
method so you can create the query.
Read and Write a single column in SQLite android
-
04-10-2022 - |
Question
I want to read a single text column from my database and afterwards update it. I created two methods below:
public boolean editNote(Lesson lesson) {
ContentValues values = new ContentValues();
values.put(LessonsDBOpenHelper.COLUMN_NOTE, lesson.getNote());
String where = LessonsDBOpenHelper.COLUMN_ID + "=" + lesson.getId();
int result = database.update(LessonsDBOpenHelper.TABLE_LESSONS, values, where, null);
return (result == 1);
}
and
public String readNote(Lesson lesson) {
String query = "SELECT * FROM lessons " +
"WHERE lessonId =" +lesson.getId();
Cursor cursor = database.rawQuery(query, null);
String returnString = ""; // default value if nothing is found
if (cursor.moveToFirst()){
returnString = cursor.getString(cursor.getColumnIndex(LessonsDBOpenHelper.COLUMN_NOTE));
}
cursor.close();
return returnString;
}
and then I call editNote() and readNote() methods from another activity like this:
public class Note extends Activity {
private static final String LOGTAG = "MYAPP";
Lesson lesson;
LessonsDataSource datasource;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_note);
Bundle b = getIntent().getExtras();
lesson = b.getParcelable(".model.Lesson");
Button noteSave = (Button) findViewById(R.id.note_save);
Button noteClear = (Button) findViewById(R.id.note_clear);
final TextView noteText = (TextView) findViewById(R.id.noteText);
String dbNote = datasource.readNote(lesson);
noteText.setText(dbNote);
Log.i(LOGTAG, "Note loaded from DB is "+dbNote);
noteClear.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
noteText.setText("");
lesson.setNote("");
datasource.editNote(lesson);
}
});
noteSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String note = noteText.getText().toString();
lesson.setNote(note);
datasource.editNote(lesson);
Log.i(LOGTAG, "Note text changed to "+note);
String dbNote = datasource.readNote(lesson);
Log.i(LOGTAG, "Note text in DB is "+dbNote);
}
});
}
}
But my app crashes when it gets to readNote or editNote methods. What am I doing wrong?
I just want to read and then update a text filed in my database.
Edit: Here is the LogCat:
01-30 02:47:29.809: D/AndroidRuntime(30850): Shutting down VM
01-30 02:47:29.809: W/dalvikvm(30850): threadid=1: thread exiting with uncaught exception (group=0x41744ba8)
01-30 02:47:29.809: E/AndroidRuntime(30850): FATAL EXCEPTION: main
01-30 02:47:29.809: E/AndroidRuntime(30850): Process: com.me.linux, PID: 30850
01-30 02:47:29.809: E/AndroidRuntime(30850): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.me.linux/com.me.linux.Note}: java.lang.NullPointerException
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.access$800(ActivityThread.java:135)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.os.Looper.loop(Looper.java:136)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 02:47:29.809: E/AndroidRuntime(30850): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 02:47:29.809: E/AndroidRuntime(30850): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 02:47:29.809: E/AndroidRuntime(30850): at dalvik.system.NativeStart.main(Native Method)
01-30 02:47:29.809: E/AndroidRuntime(30850): Caused by: java.lang.NullPointerException
01-30 02:47:29.809: E/AndroidRuntime(30850): at com.me.linux.Note.onCreate(Note.java:31)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.Activity.performCreate(Activity.java:5231)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 02:47:29.809: E/AndroidRuntime(30850): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-30 02:47:29.809: E/AndroidRuntime(30850): ... 11 more
01-30 02:59:51.589: I/MYAPP(31030): Database closed
Edit 2: Here is the code of putExtra of the intent that starts this activity:
case R.id.action_edit_note:
Intent intent = new Intent(this, Note.class);
intent.putExtra(".model.Lesson", lesson);
startActivityForResult(intent, 1002);
break;
Solution
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow