How to output a string attribute on an object that may be be null
Question
As part of a dataTable in a seam JSF page, one column requires the output of a name:
<h:outputText value="#{listing.staffMember.name}"/>
The problem is that "staffMember" may be null on some listings, so I get the error:
javax.el.ELException: /xxxxx.xhtml @42,67 value="#{listing.staffMember.name}": Error reading 'name' on type xxxx.model.AgentStaff_$$_javassist_152
If the value is null, I don't want any text rendered. I tried this:
<h:outputText value="#{listing.staffMember.name}" rendered="#{listing.staffMember != null}"/>
But the same error comes up.
How can I output a property on an object that may be null?
Solution
You could probably use the ternary operator, which would look something like:
value="#{listing.staffMember != null ? listing.staffMember.name : 'None'}"
Or you could use the c:if tag.
OTHER TIPS
Could you try this (always worked for me):
<h:outputText value="#{listing.staffMember.name}"
rendered="#{not empty listing.staffMember}"/>
Not sure what the difference is with comparing to null.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow