3

I get following error in logcat when trying to retrieve all SQLite rows from SQLiteOpenHelper.

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, null, 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, null, null, 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.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());

    // 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, KEY_ID + " = ?",
            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();
}

And in my Fragment class where 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 89th line in onCreateView is

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

Thanks in advance.

1
  • SQLiteDatabase db = this.getWritableDatabase(); change to this.getReadableDatabase();. Should not be a problem. Give a try. And check whether databaseHandlerNote is null. I think databaseHandlerNote is null. Commented Feb 9, 2015 at 15:04

2 Answers 2

5

The problem is that onActivityCreated() is called after onCreateView(). Thus your databaseHandlerNote hasn't been created yet, and trying to use it will result in a NullPointerException.

Check out the Fragment lifecycle diagram from the Fragment documentation.

Sign up to request clarification or add additional context in comments.

4 Comments

onCreate() called before onActivityCreated()
@andDev You're not using onCreate in your fragment.
i didn't add this piece here
@andDev And it doesn't matter much. You're initializing databaseHandlerNote in #onActivityCreated and this is called after #onCreateView where you try to use it.
2

From Fragment lifecircle, onCreateView() is called before onActivityCreated(), so when you call:

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

in onCreateView(), databaseHandlerNote is not yet created, then you got exception. So solution is that:

move your:

databaseHandlerNote = new DatabaseHandlerNotes(getActivity());

from onActivityCreated() to onCreate()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.