patient.getAge() returns null with patient age defined

Wondering why would patient.getAge() return null for a patient with age in search results widget and patient header; reproduced in the code below https://github.com/IsantePlus/openmrs-module-isantepluspatientdashboard/blob/master/omod/src/main/java/org/openmrs/module/isantepluspatientdashboard/fragment/controller/IsantePlusMostRecentVitalsFragmentController.java#L74

How do you tell that the return value of patient.getAge is null? Is it via the debugger, or something else?

If person.birthdate is null getAge() returns null

the patient birthdate is set am sure; @dkayiwa; debugging

Very surprising the same call doesn’t return null in another class but does in that above controller

You may want to do a sanity check of a System.out.println()

Wyclif is right, for getAge to return null the only option is birthdate to be null (primitive types can’t be null).

public Integer getAge(Date onDate) {
	if (birthdate == null) {
		return null;
	}
	
	// Use default end date as today.
	Calendar today = Calendar.getInstance();
	// But if given, use the given date.
	if (onDate != null) {
		today.setTime(onDate);
	}
	
	// If date given is after date of death then use date of death as end date
	if (getDeathDate() != null && today.getTime().after(getDeathDate())) {
		today.setTime(getDeathDate());
	}
	
	Calendar bday = Calendar.getInstance();
	bday.setTime(birthdate);
	
	int age = today.get(Calendar.YEAR) - bday.get(Calendar.YEAR);
	
	// Adjust age when today's date is before the person's birthday
	int todaysMonth = today.get(Calendar.MONTH);
	int bdayMonth = bday.get(Calendar.MONTH);
	int todaysDay = today.get(Calendar.DAY_OF_MONTH);
	int bdayDay = bday.get(Calendar.DAY_OF_MONTH);
	
	if (todaysMonth < bdayMonth) {
		age--;
	} else if (todaysMonth == bdayMonth && todaysDay < bdayDay) {
		// we're only comparing on month and day, not minutes, etc
		age--;
	}
	
	return age;
}

o, i totally agree and that’s why i was amazed at how this could happen!

Put a breakpoint in setBirthdate and you’ll see who is setting it to NULL :slight_smile:

1 Like

@k_joseph you might have to do some debugging and find out why birthdate is null because it is the only reason why Person.getAge would return null, try putting a break point in the setter for birthdate to see what is setting it to a null

1 Like

the patient object from @FragmentParam("patientId") Patient patient doesn’t seem to set some patient properties, replacing it with @RequestParam("patientId") Patient patient within my controller parameters solved this issue