I have a method that calculates age; calculateAge(User user)
:
public int calculateAge(User user) {
String date = null, month = null, year = null;
String[] fields;
String DOB = user.getDOB();
System.out.println(DOB);
fields = DOB.split("-");
System.out.println(fields);
fields[0] = date;
fields[1] = month;
fields[2] = year;
System.out.println(date);
System.out.println(month);
System.out.println(year);
LocalDate birthDate = LocalDate.of(Integer.parseInt(year), Integer.parseInt(month), Integer.parseInt(date));
LocalDate now = LocalDate.now();
Period age = Period.between(birthDate, now);
return age.getYears();
}
The prints are debugging lines, and as of now, this is what they print:
The DOB is 1-1-1988, but fields is [Ljava.lang.String;@6d41a4a instead of displaying the array of fields. As such, date, month, and year are printed as null and it can't Integer.parseInt(null)
, so it gives me a NumberFormatException: null
.