I’m having trouble using a selectOneRadio component. I get a NullPointerException in my Converter’s getAsString. That exception is thrown before I even get to see that component.
This is how it looks like:
<h:selectOneRadio id="bookA"
value="#{bookHandler.compareBookA}">
<f:converter converterId="bookConverter" />
<f:selectItems value="#{bookHandler.selectedBooks}"
var="book" itemLabel="#{book.shortname}" itemValue="#{book}" />
</h:selectOneRadio>
The property compareBookA is an object of type Book.
This is the method that throws the NPE:
@Override
public String getAsString(FacesContext fc, UIComponent uic, Object value)
throws ConverterException {
Book book = (Book) value;
if (book == null ) {
throw new ConverterException("NPE...");
}
return book.getShortname();
}
I have also overwritten toString().
For some reason the component is rendered if I change it to selectManyCheckbox (and leave the rest like it is).
I’m using JSF 2 (MyFaces implementation) with Tomahawk on Tomcat.
Bonus question: Why do I need a converter in the first place? If I leave the converter away, the component is rendered, but I want to pass the chosen book to some action method and it won’t be a Book then.
Any ideas?
Thanks!
Apparently
#{bookHandler.compareBookA}is justnull.You shouldn’t throw
ConverterExceptionwhenBookisnull, but just let it go.As to the bonus question, you need converters because the HTML response is basically one large string. All Java objects needs to be converted to string to get embedded in the HTML response. Also, the HTTP request parameters are by default strings. The
request.getParameter()returnsString. HTML/HTTP has totally no notion of how to handle and pass fullworthy Java objects.See also: