0
public static final String dbName="notesDb";
public static final Integer DB_version=1;

public static final String key_id="id";
public static final String key_title="title";
public static final String key_subject="subject";

public static final String Table_notes="notes";

public DbNotes(Context context) {
    super(context, dbName, null, DB_version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    String create_table="create table "+Table_notes+"("+key_id+" Integer primary key,"+key_title+"  varchar(30), "+key_subject+" varchar(50)) ";
    db.execSQL(create_table);
}

public Note getNoteByID(int id){
    SQLiteDatabase db=this.getReadableDatabase();
    String selectQuery="SELECT * FROM "+Table_notes+" WHERE id="+id;
    Cursor cursor=db.rawQuery(selectQuery,null);

    Note note=null;
    if (cursor.moveToFirst()){
        String title=cursor.getString(cursor.getColumnIndex(key_title));
        String subject=cursor.getString(cursor.getColumnIndex(key_subject));
        int id_item=cursor.getInt(cursor.getColumnIndex(key_id));
        note=new Note(title,subject,id_item);
    }
    return note;
}

logcat message:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.ebda3.notes.Note.getTitle()' on a null object reference
Taslim Oseni
  • 6,086
  • 10
  • 44
  • 69
Alaa
  • 21
  • 4
  • I believe that your `getNoteByID` method returning null – ronginat Mar 22 '19 at 11:41
  • When the id does not exist in the table, the method `getNoteByID()` returns `null`. So before calling `getTitle()` check if the `Note` object is `null` to avoid NPE. – forpas Mar 22 '19 at 12:00

1 Answers1

0

Try this also

Cursor res = db.rawQuery("select * from "+TABLE_NAME+" where ID = " + id,null); if above solution not working then try this also

one time uninstall your app and re-install hope this helps you

Urvish Jani
  • 104
  • 1
  • 4
  • you are right , getID method return 0 value for every item in ListView public void onItemClick(AdapterView> parent, View view, int position, long id) { Intent intent=new Intent(getApplicationContext(),UpdateNote.class); Note current_note=(Note)parent.getItemAtPosition(position); intent.putExtra("id",current_note.getId()); startActivity(intent); – Alaa Mar 23 '19 at 09:30