"conversion error setting value for 'null converter' - why do i need a converter in jsf?" Code Answer



jsf generates html. html is in java terms basically one large string. to represent java objects in html, they have to be converted to string. also, when a html form is submitted, the submitted values are treated as string in the http request parameters. under the covers, jsf extracts them from the httpservletrequest#getparameter() which returns string.

to convert between a non-standard java object (i.e. not a string, number or boolean for which el has builtin conversions, or date for which jsf provides builtin <f:convertdatetime> tag), you really have to supply a custom converter. the selectitem has no special purpose at all. it's just a leftover from jsf 1.x when it wasn't possible to supply e.g. list<warehouse> directly to <f:selectitems>. it has also no special treatment as to labels and conversion.


you need to implement getasstring() method in such way that the desired java object is been represented in an unique string representation which can be used as http request parameter. normally, the technical id (the database primary key) is used here.

public string getasstring(facescontext context, uicomponent component, object modelvalue) {
    if (modelvalue == null) {
        return ""; // never return null here!

    if (modelvalue instanceof warehouse) {
        return string.valueof(((warehouse) modelvalue).getid());
    } else {
        throw new converterexception(new facesmessage(modelvalue + " is not a valid warehouse"));

note that returning an empty string in case of a null/empty model value is significant and required by the javadoc:

returns: a zero-length string if value is null, otherwise the result of the conversion

otherwise the generated <option> will not have a value attribute and by default send the item label back into getasobject(). see also using a "please select" f:selectitem with null/empty value inside a p:selectonemenu.


you need to implement getasobject() in such way that exactly that string representation as returned by getasstring() can be converted back to exactly the same java object specified as modelvalue in getasstring().

public object getasobject(facescontext context, uicomponent component, string submittedvalue) {
    if (submittedvalue == null || submittedvalue.isempty()) {
        return null;

    try {
        return warehouseservice.find(long.valueof(submittedvalue));
    } catch (numberformatexception e) {
        throw new converterexception(new facesmessage(submittedvalue + " is not a valid warehouse id"), e);

in other words, you must be technically able to pass back the returned object as modelvalue argument of getasstring() and then pass back the obtained string as submittedvalue argument of getasobject() in an infinite loop.


finally just annotate the converter with @facesconverter to hook on the object type in question, jsf will then automatically take care of conversion when warehouse type ever comes into the picture:


that was the "canonical" jsf approach. it's after all not very effective as it could indeed also just have grabbed the item from the <f:selectitems>. but the most important point of a converter is that it returns an unique string representation, so that the java object could be identified by a simple string suitable for passing around in http and html.

generic converter based on tostring()

jsf utility library omnifaces has a selectitemsconverter which works based on tostring() outcome of the entity. this way you do not need to fiddle with getasobject() and expensive business/database operations anymore. for some concrete use examples, see also the showcase.

to use it, just register it as below:

<h:selectonemenu ... converter="omnifaces.selectitemsconverter">

and make sure that the tostring() of your warehouse entity returns an unique representation of the entity. you could for instance directly return the id:

public string tostring() {
    return string.valueof(id);

or something more readable/reusable:

public string tostring() {
    return "warehouse[id=" + id + "]";

see also:

  • how to populate options of h:selectonemenu from database?
  • generic jsf entity converter - so that you don't need to write a converter for every entity.
  • using enums in jsf selectitems - enums needs to be treated a bit differently
  • how to inject @ejb, @persistencecontext, @inject, @autowired, etc in @facesconverter?

unrelated to the problem, since jsf 2.0 it's not explicitly required anymore to have a list<selectitem> as <f:selectitem> value. just a list<warehouse> would also suffice.

<h:selectonemenu value="#{bean.selectedwarehouse}">
    <f:selectitem itemlabel="choose one .." itemvalue="#{null}" />
    <f:selectitems value="#{bean.availablewarehouses}" var="warehouse"
        itemlabel="#{warehouse.name}" itemvalue="#{warehouse}" />
private warehouse selectedwarehouse;
private list<warehouse> availablewarehouses;
By maudulus on May 21 2022

Answers related to “conversion error setting value for 'null converter' - why do i need a converter in jsf?”

Only authorized users can answer the Search term. Please sign in first, or register a free account.