0

The following method throws java.lang.NullPointerException

public ArrayList<User> getDinner() throws ParseException {

DateFormat formatter = new SimpleDateFormat("dd/MM/yy");
Date date = formatter.parse(bookingdate);
Date today = formatter.parse(formatter.format(new Date())); // 

ArrayList<User> subset = new ArrayList<User>();
for (User user : userList) {

    if (((date.equals(today) || date == null)) && (1700 < user.bookingtime)) {
        subset.add(user);
    }

}
return subset;
}

How is this caused and how can I solve it?

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Baz627
  • 11
  • 3
  • Go through the log and find the line number where you are getting the NullPointerException. What did you try? – srk Mar 08 '15 at 14:50
  • You should do null checks first but here in this case the `Date` object among others is initialized before the conditional check. The `java.lang.NullPointerException` should be thrown from somewhere else. (but all in all this is not related JSF by the way) – Tiny Mar 08 '15 at 16:18

1 Answers1

1

Not much to go on, but this is surely wrong:

date.equals(today) || date == null

You should really switch the order of these two, otherwise (if your date is null), you will get an NullPointerException.

Gergely Bacso
  • 14,243
  • 2
  • 44
  • 64