Java – Android NullPointerException when trying to retrieve data from sqliteopenhelper

When trying to retrieve all SQLite rows from sqliteopenhelper, I encountered the following error in logcat

Caused by: java.lang.NullPointerException at notes.dev.tauhid.com.mynotes.fragment.MyNotes.onCreateView(MyNotes.java:89)

My sqliteopenhelper class is

public class DatabaseHandlerNotes extends sqliteOpenHelper {

private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "my_notes";
private static final String TABLE_NOTES = "my_notes_table";
private static final String KEY_ID = "id";
private static final String KEY_TITLE = "title";
private static final String KEY_DESCRIPTION = "phone_number";
private static final String KEY_DATE = "date";
private static final String KEY_REMINDER_DATE = "reminder_date";
private static final String KEY_CATEGORY = "category";
private static final String KEY_LOCK = "lock";

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

@Override
public void onCreate(sqliteDatabase db) {
    String CREATE_NOTES_TABLE = "CREATE TABLE " + TABLE_NOTES + "("
            + KEY_ID + " INTEGER PRIMARY KEY," + KEY_TITLE + " TEXT,"
            + KEY_DESCRIPTION + " TEXT," + KEY_DATE + " INTEGER," + KEY_REMINDER_DATE + " INTEGER," + KEY_CATEGORY + " INTEGER," + KEY_LOCK + " TEXT" + ")";
    db.execsql(CREATE_NOTES_TABLE);
}

@Override
public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) {
    db.execsql("DROP TABLE IF EXISTS " + TABLE_NOTES);

    onCreate(db);
}

public void addNote(Note note) {
    sqliteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE,note.getTitle());
    values.put(KEY_DESCRIPTION,note.getDescription());
    values.put(KEY_DATE,note.getDate());
    values.put(KEY_REMINDER_DATE,note.getReminderDate());
    values.put(KEY_CATEGORY,note.getCategory());
    values.put(KEY_LOCK,note.getLock());

    db.insert(TABLE_NOTES,values);
    db.close();
}

public Note getNote(int id) {
    sqliteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_NOTES,new String[] { KEY_ID,KEY_TITLE,KEY_DESCRIPTION,KEY_DATE,KEY_REMINDER_DATE,KEY_CATEGORY },KEY_ID + "=?",new String[] { String.valueOf(id) },null);
    if (cursor != null)
        cursor.moveToFirst();

    Note note = new Note(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),Integer.parseInt(cursor.getString(3)),Integer.parseInt(cursor.getString(4)),Integer.parseInt(cursor.getString(5)),cursor.getString(6));
    return note;
}

public List<Note> getAllNotes() {
    List<Note> noteList = new ArrayList<Note>();
    String selectQuery = "SELECT * FROM " + TABLE_NOTES;

    sqliteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery,null);

    if (cursor.moveToFirst()) {
        do {
            Note note = new Note();
            note.setID(Integer.parseInt(cursor.getString(0)));
            note.setTitle(cursor.getString(1));
            note.setDescription(cursor.getString(2));
            note.setDate(Integer.parseInt(cursor.getString(3)));
            note.setReminderDate(Integer.parseInt(cursor.getString(4)));
            note.setCategory(Integer.parseInt(cursor.getString(5)));
            note.setLock(cursor.getString(6));
            noteList.add(note);
        } while (cursor.moveToNext());
    }
    return noteList;
}

public int updateNote(Note note) {
    sqliteDatabase db = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_TITLE,note.getLock());

    // updating row
    return db.update(TABLE_NOTES,values,KEY_ID + " = ?",new String[] { String.valueOf(note.getID()) });
}

public void deleteNote(Note note) {
    sqliteDatabase db = this.getWritableDatabase();
    db.delete(TABLE_NOTES,new String[] { String.valueOf(note.getID()) });
    db.close();
}

public int getNotesCount() {
    String countQuery = "SELECT  * FROM " + TABLE_NOTES;
    sqliteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery,null);
    cursor.close();

    return cursor.getCount();
}

In my fragment class, I want to retrieve all rows

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    databaseHandlerNote = new DatabaseHandlerNotes(getActivity());

}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.my_notes_fragment_notes,container,false);
    ListView allNotes = (ListView) rootView.findViewById(R.id.my_notes_all);
    List<Note> noteList = databaseHandlerNote.getAllNotes();
    for (Note note : noteList) {
        Note noteEach = new Note();
        noteEach.setID(note.getID());
        noteEach.setTitle(note.getTitle());
        noteEach.setDescription(note.getDescription());
        noteEach.setCategory(note.getCategory());
        noteEach.setLock(note.getLock());
        noteEach.setDate(note.getDate());
        noteEach.setReminderDate(note.getReminderDate());
        this.customNotesList.add(noteEach);
    }
    customNoteAdapter = new CustomNoteAdapter(getActivity(),customNotesList);
    allNotes.setAdapter(customNoteAdapter);
    return rootView;
}

Here, line 89 oncreateview is

List<Note> noteList = databaseHandlerNote.getAllNotes();

Thank you in advance

Solution

The problem is to call onActivityCreated () after onCreateView (). Therefore, the databasehandlernote has not been created and attempting to use it will result in NullPointerException

View the fragment lifecycle diagram from the fragment document

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>