"conversion error setting value '52' for 'null converter' " Code Answer

5

you can't use a custom type on the <h:selectonemenu/> component (or any of the <h:selectxxxx/>) without creating a jsf converter. converters exist in jsf to assist with translating essentially custom or sever-side items/constructs into web page friendly/human readable formats and also to be able to save selections from the client side back to the server side. so what the error message is essentially telling you there is that it cannot make sense enough of the value 52 submitted from the client side to save it to the server side as a category object/type

so what you're required to do is to create an implementation of a jsf converter that essentially helps jsf make sense of your category object.

here's a rough estimation of what you need to do:

  1. implement a converter for your category type:

    // you must annotate the converter as a managed bean, if you want to inject
    // anything into it, like your persistence unit for example.
    @managedbean(name = "categoryconverterbean") 
    @facesconverter(value = "categoryconverter")
    public class categoryconverter implements converter {
    
        @persistencecontext(unitname = "luavipupu")
        // i include this because you will need to 
        // lookup  your entities based on submitted values
        private transient entitymanager em;  
    
        @override
        public object getasobject(facescontext ctx, uicomponent component,
                string value) {
          // this will return the actual object representation
          // of your category using the value (in your case 52) 
          // returned from the client side
          return em.find(category.class, new biginteger(value)); 
        }
    
        @override
        public string getasstring(facescontext fc, uicomponent uic, object o) {
            //this will return view-friendly output for the dropdown menu
            return ((category) o).getid().tostring(); 
        }
    }
    
  2. reference your new converter in your <h:selectonemenu/>

    <h:selectonemenu id="category_fk" 
      converter="#{categoryconverterbean}"
      value="#productcontroller.product.category_fk}" 
      title="category_fk" >
    <!-- done: update below reference to list of available items-->
        <f:selectitems value="#{productcontroller.categorylist}" 
          var="prodcat" itemvalue="#{prodcat.category_id}" 
          itemlabel="#{prodcat.name}"/>
    </h:selectonemenu>
    

we're using the name categoryconverterbean because we want to take advantage of the entity manager trick you pulled in the converter. any other case, you would've used the name you set on the converter annotation.

By Luckylazuli on August 16 2022

Answers related to “conversion error setting value '52' for 'null converter' ”

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