Enumeration NullPointerException

Hello,

I’ve got an issue getting table data to print a custom page. I’m extracting all data from a Table in JobBrowse.java controller.

For instance:

Job job = jobsTable.getSingleSelected();
String documentFileType = job.getMiscDocumentFileTypeEnum().getId() != null ? job.getMiscDocumentFileTypeEnum().getId() : " ";

If the enumeration field has not yet been selected, the client may still need to print this data, but I’m getting a NullPointerException.

I had a similar problem with Associations:

String preparedBy = job.getPreparedBy().getName() != null ? job.getPreparedBy().getName() : " ";

I was able to get around this with the following:

User preparedBy = job.getPreparedBy() != null ? job.getPreparedBy() : new User();
String preparedByName = preparedBy.getName() != null ? preparedBy.getName() : " ";

But honestly, even after playing around with the options available, I can’t seem to find a way to do this with Enumeration type fields that have yet to be filled out.

I’d really appreciate any information of how to avoid this NPE issue while still allowing me to work with the data in this fashion for the custom exportDisplay method of printing this document.

I’ve had problems getting the Reporting add-on functioning correctly, and need to do it this way as a work around.

Thanks in advance!

I’ve still had no luck in figuring this out. I’d love a simple solution. I’m sure it’s out there, I’m just not familiar enough with working in this environment.

Add an ‘empty’ value to your enumeration and initiate the entity field with this value. You need to add a validation to this field in case a field is mandatory.
This could be 0 or -1 in case of integer enumeration and empty string in case of string enumeration.
Same solution will work with associations.

Actually, come to think of it, this option might not work given that data is already entered in the application and these values aren’t necessarily set. Hmmm. Darn it!

I’m still very much hoping for some assistance from the CUBA developers. If all Enumeration type fields are filled out, there is no issue, but if any are null, my validation check seems to be ignored.

Hi,

This code should be changed in order to be null-safe to the following:

String documentFileType = job.getMiscDocumentFileTypeEnum() != null 
           ? job.getMiscDocumentFileTypeEnum().getId() : " ";
2 Likes

This is exactly the solution I needed. I will remember to handle in this fashion moving forward. Thank you!