Exception with Convert from a Primefaces selectOneMenu

Asked

Viewed 869 times

1

I created a dynamic selectOneMenu that takes a Author Object List that implements Serializable. The code is as follows::

XHTML from selectOneMenu

<p:selectOneMenu id="autor_selecionado" value="#{pragaBean.autor}" style="width: 19%;" 
                                         required="true" 
                                         requiredMessage="Selecione um Autor">
                            <f:selectItem itemLabel="Selecione um autor" itemValue="" />
                            <f:selectItems   value="#{autorBean.autores}" var="autor"
                                             itemLabel="#{autor.nome}" 
                                             itemValue="#{autor}" />
                        </p:selectOneMenu>

Convert Class

public class ClasseConverters {

    @FacesConverter(forClass = Autor.class)
    public class AutorConverter implements Converter {

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
            if (value != null && !value.isEmpty()) {
                return (Autor) uiComponent.getAttributes().get(value);
            }
            return null;
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
            if (value instanceof Autor) {
                Autor entity = (Autor) value;
                if (entity instanceof Autor && entity.getId() != null) {
                    uiComponent.getAttributes().put(entity.getId().toString(), entity);
                    return entity.getId().toString();
                }
            }
            return "";
        }
    }

    //outros converters ...
}

Exception that occurs when loading page

java.lang.InstantiationException: pacote.util.ClasseConverters$AutorConverter
    at java.lang.Class.newInstance(Class.java:423)
    at com.sun.faces.application.ApplicationImpl.newConverter(ApplicationImpl.java:1829)
    at com.sun.faces.application.ApplicationImpl.createConverter(ApplicationImpl.java:1361)
    at org.primefaces.renderkit.InputRenderer.findImplicitConverter(InputRenderer.java:163)
    at org.primefaces.renderkit.InputRenderer.getOptionAsString(InputRenderer.java:146)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeOption(SelectOneMenuRenderer.java:353)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeSelectItems(SelectOneMenuRenderer.java:339)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeInput(SelectOneMenuRenderer.java:123)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeMarkup(SelectOneMenuRenderer.java:93)
    at org.primefaces.component.selectonemenu.SelectOneMenuRenderer.encodeEnd(SelectOneMenuRenderer.java:67)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:312)
    at com.sun.faces.renderkit.html_basic.GridRenderer.renderRow(GridRenderer.java:185)
    at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:129)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1856)
    at javax.faces.render.Renderer.encodeChildren(Renderer.java:176)
    at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:889)
    at org.primefaces.renderkit.CoreRenderer.renderChild(CoreRenderer.java:81)
    at org.primefaces.renderkit.CoreRenderer.renderChildren(CoreRenderer.java:68)
    at org.primefaces.component.layout.LayoutUnitRenderer.encodeEnd(LayoutUnitRenderer.java:49)
    at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:919)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1863)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1859)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:456)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:133)
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:120)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:219)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:647)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:136)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:526)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1078)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:655)
    at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NoSuchMethodException: pacote.util.ClasseConverters$AutorConverter.<init>()
    at java.lang.Class.getConstructor0(Class.java:3074)
    at java.lang.Class.newInstance(Class.java:408)
    ... 56 more

2 answers

1


Try to use a converter for each entity as follows:

@FacesConverter(value = "ConversorAutor")
public class AutorConverter implements Converter {

In your xhtml use the convert property in selectOneMenu. It will look like this:

                   <p:selectOneMenu id="autor_selecionado" value="#{pragaBean.autor}" style="width: 19%;" 
                                     converter="ConversorAutor"
                                     required="true" 
                                     requiredMessage="Selecione um Autor">
                        <f:selectItem itemLabel="Selecione um autor" itemValue="" />
                        <f:selectItems   value="#{autorBean.autores}" var="autor"
                                         itemLabel="#{autor.nome}" 
                                         itemValue="#{autor}" />
                    </p:selectOneMenu>

I have never used multiple converters within a single class, this may be your problem.

Hugs

  • The error continues. :(

  • 1

    Try to create a single convert class for your entity. Here we use it like this! Try it, it might work! D

  • It is also strange to me this type of architecture, with more than one convert within a single class.

  • Thank you, really that was it. It ran, Gilvan reply as reply to mark as solved. grateful!

  • Hello. I edited this answer even. So you can mark this one as correct! Thanks and hugs

1

I know the question is old, but here’s an explanation that’s been missing: The way the convert classes were declared, they are internal classes of ClasseConverters. An object of an inner class can only be instantiated if it is associated with an external class object (internal classes have access private to external class objects). Ex:

ClasseConverters converters = new ClasseConverters();
ClasseConverters.AutorConverter autorConverter = converters.new AutorConverter();

JSF, however, will not create an external class object, so Exception.

To solve, internal classes need to be static. Static internal classes need not be associated with an external class object:

public class ClasseConverters {

    @FacesConverter(forClass = Autor.class)
    public static class AutorConverter implements Converter { //static

        @Override
        public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
            if (value != null && !value.isEmpty()) {
                return (Autor) uiComponent.getAttributes().get(value);
            }
            return null;
        }

        @Override
        public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
            if (value instanceof Autor) {
                Autor entity = (Autor) value;
                if (entity instanceof Autor && entity.getId() != null) {
                    uiComponent.getAttributes().put(entity.getId().toString(), entity);
                    return entity.getId().toString();
                }
            }
            return "";
        }
    }

    //outros converters ...
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.