So, I'm creating a photo library and I created a method that when you click on a photo in the list, it should display a few properties of it such as caption, tags, and date. It's called handleList but it's throwing a null pointer exception when I click on a part of the list that's not populated. I had a line of code that should check whether an item was selected but I don't think it's working because the code under it is still being run and it's throwing null pointer exception because it's trying to draw data from a photo that isn't there therefore it's a null pointer exception. However, it does work when I click on a photo so maybe I have the wrong type of check. Any ideas on how to fix my code? I think I have to change the mouse event parameter or write a working check that checks is something is selected.
Here's the line where it breaks:
Date date1 = p.dop.getTime();
Here's the code below:
**
* This will populate photo labels based on if an item is selected
* @param e Mouse Click
*/
public void handleList(MouseEvent e) {
if(photos.getSelectionModel().selectedItemProperty()!=null){
Photo p = photos.getSelectionModel().getSelectedItem();
DateFormat dateFormat = new SimpleDateFormat("MM-dd-yyyy");
Date date1 = p.dop.getTime();
String strDate1 = dateFormat.format(date1);
dateLabel.setText(strDate1);
deleteTag.getItems().clear();
tagList.getItems().clear();
for(Tag t: p.tags)
{
String s=t.name+"-"+t.val;
if(!deleteTag.getItems().contains(s))
deleteTag.getItems().add(s);
if(!tagList.getItems().contains(s))
tagList.getItems().add(s);
}
try
{
UserList.write(userlist);
} catch (Exception i) {
// TODO Auto-generated catch block
i.printStackTrace();
}
}
}
can someone please explain why this line below isn't working:
photos.getSelectionModel().selectedItemProperty()!=null