tags:

views:

160

answers:

2

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?

+5  A: 

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.

Fabian Steeg
+3  A: 

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.

ivans
To my understanding, the Rendered value doesn't stop the value expression from being evaluated. The Value expression is evaluated, then, when it's time to render the view, if the render expression is false, it doesn't make it to the view.
Drew