I'm currently developing a Log class called NotesHistoryController, which contains items from the type NotepadHistory. NotepadHistory objects contain the changed Notepad object, a timestamp and the logged in user who changed the note object.
Notepads must be prioritized, such as "good" or "Bad" etc. and the priority is recognizable on the color which is displayed in the ListView i.e. defined in the cellFactory.
The way how I'm creating NotepadHistory objects is like this: User changes the current note object, db is updating the old object and then I'm defining / creating a NotepadHistory object with its attributes + a foreign reference to the changed Notepad object:
Dao<Notepad, Integer> notepadDao = db.getNotepadDao();
Dao<NotepadHistory, Integer> notepadHistoryDao = db.getNotepadHistoryDao();
NotepadHistory notepadHistory = new NotepadHistory();
//Saving the Notepad which is to be edited to delete it from the listView in the main window
Notepad ersatz = this.notepad;
Timestamp timestamp = new Timestamp(System.currentTimeMillis());
//Saving the notepad after edits
this.notepad.setNotepadName(noteName);
this.notepad.setClassification(priority);
this.notepad.setNotepadContent(textContent);
try {
notepadHistory.setNotepad(this.notepad);
notepadHistory.setTimestamp(timestamp);
notepadHistory.setUser(db.getLoggedInUser());
notepadHistoryDao.create(notepadHistory);
notepadDao.update(this.notepad);
Now there is a button which after being clicked, should show a ListView with NotepadHistory objects. All of this happens in the NotesHistoryController class. Here is my issue: After clicking on the history button in the application, it is giving me a NullPointerException in my cellFactory. After debugging I found out, that the referenced notepad object of the current NotepadHistory object doesn't contain any listings except the notepad Id. After hours of debugging etc., I still don't know where the problem lays. This is my cellFactory:
notesHistoryListView.getItems().addAll(db.getNotepadHistoryDao().queryForAll());
notesHistoryListView.setCellFactory(new Callback<ListView<NotepadHistory>, ListCell<NotepadHistory>>() {
public ListCell<NotepadHistory> call(ListView<NotepadHistory> param) {
return new ListCell<NotepadHistory>() {
@Override
protected void updateItem(NotepadHistory item, boolean empty) {
super.updateItem(item, empty);
String style = "";
if (!empty && item != null) {
setText(item.getNotepad().getNotepadName());
switch (item.getNotepad().getClassification()) {
case GOOD:
style = "-fx-background-color: green";
break;
case MEDIUM:
style = "-fx-background-color: yellow";
break;
case BAD:
style = "-fx-background-color: red";
break;
case NEUTRAL:
style = "-fx-background-color: grey";
break;
}
} else {
setText("");
}
setStyle(style);
}
};
}
});
The weird thing is, that it doesn't give me an exception for the setText(item.getNotepad().getNotepadName()); line. It's just for line switch (item.getNotepad().getClassification()).