Error to play data from the search screen for registration screen for editing

0

Gentlemen, I'm having trouble getting the objs from my client search screen, and playing on the registration screen for editing the same.

My search screen has a button with a

<p:button outcome="/cliente/CadastroCliente.xhtml" icon="ui-incon-pencil" title="Editar" >
                        <f:param name="cliente" value="#{cliente.id}"/>

My customer registration page, where you will receive the parameter is thus. For I am using omnifaces.

        <f:metadata>
            <o:viewParam name="cliente" value="#{cadastroClienteBean.cliente}"/>
        </f:metadata>

My Repository class:

    @SuppressWarnings("unchecked")
    public List<Cliente> filtrados(ClienteFilter filtro) {
        Session session = manager.unwrap(Session.class);
        Criteria criteria = session.createCriteria(Cliente.class);

        if (StringUtils.isNotBlank(filtro.getCpf())) {
            criteria.add(Restrictions.eq("cpf", filtro.getCpf()));
        }

        if (StringUtils.isNotBlank(filtro.getNome())) {
            criteria.add(Restrictions.ilike("nome", filtro.getNome(), MatchMode.ANYWHERE));
        }

        return criteria.addOrder(Order.asc("nome")).list();
    }

    public Cliente porId(Long id) {
        return manager.find(Cliente.class, id);
    }
}

ClientClientConverter

@FacesConverter(forClass = Cliente.class)
public class ClienteConverter implements Converter{

        //@Inject
        private Clientes clientes;

        public ClienteConverter() {
            clientes = CDIServiceLocator.getBean(Clientes.class);
        }

        @Override
        public Object getAsObject(FacesContext context, UIComponent component, String value) {
            Cliente retorno = null;

            if (value != null) {
                Long id = new Long(value);
                retorno = clientes.porId(id);
            }

            return retorno;
        }

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value != null) {
                //Cliente cliente = (Cliente) value;
                //return cliente.getId() == null ? null : cliente.getId().toString();
                return ((Cliente) value).getId().toString();
            }

            return "";
        }

    }

However, when my page is executed, I click the edit button that has to send the values to the registration page, the following error is returned.

mar 01, 2018 9:34:41 AM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
GRAVE: Error Rendering View[/cliente/CadastroCliente.xhtml]
java.lang.NullPointerException
    at com.moto.converter.ClienteConverter.getAsString(ClienteConverter.java:39)
    at javax.faces.component.UIViewParameter.getStringValueFromModel(UIViewParameter.java:378)
    at javax.faces.component.UIViewParameter.getStringValue(UIViewParameter.java:327)
    at javax.faces.component.UIViewParameter.encodeAll(UIViewParameter.java:313)
    at javax.faces.component.UIViewRoot.encodeViewParameters(UIViewRoot.java:1655)
    at javax.faces.component.UIViewRoot.encodeEnd(UIViewRoot.java:996)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:439)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:286)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:286)
    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:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

2018-03-01 09:34:41,578 ERROR [com.moto.util.jsf.JsfExceptionHandler] Erro de sistema: null
java.lang.NullPointerException
    at com.moto.converter.ClienteConverter.getAsString(ClienteConverter.java:39)
    at javax.faces.component.UIViewParameter.getStringValueFromModel(UIViewParameter.java:378)
    at javax.faces.component.UIViewParameter.getStringValue(UIViewParameter.java:327)
    at javax.faces.component.UIViewParameter.encodeAll(UIViewParameter.java:313)
    at javax.faces.component.UIViewRoot.encodeViewParameters(UIViewRoot.java:1655)
    at javax.faces.component.UIViewRoot.encodeEnd(UIViewRoot.java:996)
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1786)
    at com.sun.faces.application.view.FaceletViewHandlingStrategy.renderView(FaceletViewHandlingStrategy.java:439)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:124)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:286)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:286)
    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:139)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:594)
    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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:217)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:673)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Thread.java:748)

Please help me, I do not know where my error is. I'm running out of options; att.

    
asked by anonymous 01.03.2018 / 14:54

1 answer

0

The Cliente.getId() method is returning null in its getAsString() method. Calling toString() gives NullPointerException .

Change your method getAsString() :

        @Override
        public String getAsString(FacesContext context, UIComponent component, Object value) {
            if (value == null) return "";
            Long id = ((Cliente) value).getId();
            return id == null ? "" : id.toString();
        }
    
01.03.2018 / 15:34