0

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
mina
  • 1
  • 1
  • 2
  • I kind of get it. I'm not too familiar with listener to be honest. I don't know how to use listener to set certain labels when the selection is changed? I also am not sure if listener would prevent a null pointer exception when someone clicks on the listview and not on a specific item. If you could help me figure out the listener code, that would be great. I essentially need to do everything I did in handleList – mina Apr 17 '20 at 04:37
  • I am not 100% sure the code will work for you. Where is your `ListView` `CellFactory` code? – SedJ601 Apr 17 '20 at 04:50
  • repeating part of my comment from the question you deleted: [mcve] please .. (the formatting is fine now, thanks for taking care :) – kleopatra Apr 17 '20 at 09:29
  • `photos.getSelectionModel().selectedItemProperty()` is never null. – James_D Apr 17 '20 at 16:43
  • Have you tried a null check on `Photo`? Validate that `p != null` – Dustin Apr 21 '20 at 12:59

0 Answers0