NullPointerException error when saving object [closed]

0

I select the object through the bean and the link in the data table change in a dialog and when giving the submit of the dialog I get the nullPoiterException error. Would you know why?

Bean code

@Named
@ViewScoped
public class PesquisaPrevisaoBean implements Serializable {
private static final long serialVersionUID = 1L;

@Inject
private Historicos previsoes;

private List<Historico> listaDePrevisoes;

private Historico previsaoSelecionada;

private CadastroHistoricoService cadastroHistoricoService;

private PrevisaoLazyList lazyPrevisoes;

public List<Historico> getPrevisoes() {
    return listaDePrevisoes;
}

public void inicializar() {
    // carregarListaHistoricos();
    lazyPrevisoes = new PrevisaoLazyList(previsoes);

}


public void carregarListaHistoricos() {
    listaDePrevisoes = previsoes.carregaHistorico();
}

public void excluir() {
    previsoes.remover(previsaoSelecionada);
    this.listaDePrevisoes.remove(previsaoSelecionada);
    FacesUtil.addInfoMessage("Provisão " + " excluída com sucesso.");
}

public void alterarPrevisao() {

    FacesUtil.addInfoMessage("Provisão " + previsaoSelecionada.getProduto().getDescricao());
    System.out.println("Provisão " + previsaoSelecionada.getProduto().getDescricao());
    cadastroHistoricoService.salvar(previsaoSelecionada);
}

public Historico getPrevisaoSelecionada() {
    return previsaoSelecionada;
}

public void setPrevisaoSelecionada(Historico previsaoSelecionada) {
    this.previsaoSelecionada = previsaoSelecionada;
}

public PrevisaoLazyList getLazyModel() {
    return lazyPrevisoes;
}

}

Code from the list where I select the object and send it to the dialog

<p:dataTable id="historicosTable"
                value="#{pesquisaPrevisaoBean.lazyModel}" var="historico"
                style="margin-top: 20px" emptyMessage="#{msg.nenhum_tecnico}"
                rowKey="historico.codigo" selectionMode="single"
                selection="#{pesquisaHistoricosBean.historicoSelecionada}"
                paginatorPosition="bottom" rows="5" paginator="true"
                paginatorTemplate="{RowsPerPageDropdown} {PreviousPageLink} {CurrentPageReport} {NextPageLink}"
                currentPageReportTemplate="(#{msg.pagina} {currentPage} #{msg.de} {totalPages}) - (#{msg.registro} {startRecord} #{msg.a} {endRecord}) - Total ({totalRecords} #{msg.registros})"
                styleClass="mystyle" paginatorAlwaysVisible="true"
                rowsPerPageTemplate="10, 20, 30" lazy="true">

                <f:facet id="header" name="header">
               Previsão de Vendas
            </f:facet>


                <p:column headerText=" Codigo do Produto"
                    sortBy="#{historico.produto.codigoProduto}"
                    style="text-align: center; width: 140px">

                    <p:commandLink value="#{historico.produto.codigoProduto}"
                        update=":userDetailForm:display" oncomplete="userDialog.show()"
                        title="View">
                        <f:setPropertyActionListener value="#{historico}"
                            target="#{pesquisaPrevisaoBean.previsaoSelecionada}" />
                    </p:commandLink>
                </p:column>

Dialog Code

<p:dialog header="User Detail" widgetVar="userDialog"
            resizable="false" id="userDlg">
            <h:form id="userDetailForm">
                <p:panelGrid id="display" columns="2" cellpadding="4"
                    style="margin:0 auto;">
                    <h:outputText value="Previsao:"></h:outputText>

                    <h:inputText
                        value="#{pesquisaPrevisaoBean.previsaoSelecionada.produto.quantidadeRecente}"
                        required="true" requiredMessage="Please Enter Name!" />


                    <f:facet name="footer">
                        <p:commandButton id="alterar" value="Alterar"
                            update=":frmPesquisa:historicosTable" 
                            oncomplete="tratarAssociacao(args)"
                            actionListener="#{pesquisaPrevisaoBean.alterarPrevisao}" />
                    </f:facet>
            </p:panelGrid>
        </h:form>
</p:dialog>

Console Output - Error Message:

jun 01, 2017 3:34:59 PM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: 'java.lang.NullPointerException' recebido ao invocar escuta de ação '#{pesquisaPrevisaoBean.alterarPrevisao}' para o componente 'alterar'
jun 01, 2017 3:34:59 PM javax.faces.event.MethodExpressionActionListener processAction
GRAVE: java.lang.NullPointerException
    at br.com.previsao.controller.PesquisaPrevisaoBean.alterarPrevisao(PesquisaPrevisaoBean.java:71)
    at br.com.previsao.controller.PesquisaPrevisaoBean$Proxy$_$$_WeldClientProxy.alterarPrevisao(PesquisaPrevisaoBean$Proxy$_$$_WeldClientProxy.java)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.__invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.apache.el.parser.AstValue.invoke(AstValue.java:247)
    at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:267)
    at org.jboss.weld.util.el.ForwardingMethodExpression.invoke(ForwardingMethodExpression.java:39)
    at org.jboss.weld.el.WeldMethodExpression.invoke(WeldMethodExpression.java:50)
    at javax.faces.event.MethodExpressionActionListener.processAction(MethodExpressionActionListener.java:153)
    at javax.faces.event.ActionEvent.processListener(ActionEvent.java:88)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:775)
    at javax.faces.component.UICommand.broadcast(UICommand.java:300)
    at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:786)
    at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1251)
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.primefaces.webapp.filter.FileUploadFilter.doFilter(FileUploadFilter.java:79)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:118)
    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:84)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:103)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:54)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:45)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:183)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.__invoke(StandardContextValve.java:96)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
    at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.lang.Thread.run(Unknown Source)
    
asked by anonymous 01.06.2017 / 19:51

1 answer

0

The error occurs because the cadastroHistoricoService variable is not initialized or injected at any time.

So while doing this:

public void alterarPrevisao() {
    /* Trecho de código omitido */
    cadastroHistoricoService.salvar(previsaoSelecionada);
}

You will receive NullPointerException .

Inject or initialize the feature before using it:

@Inject
private CadastroHistoricoService cadastroHistoricoService;

Or

@PostConstruct
public void init() {
    this.cadastroHistoricoService = new CadastroHistoricoService();
}

Or

public PesquisaPrevisaoBean() {
    this.cadastroHistoricoService = new CadastroHistoricoService();
}

Or:

public void alterarPrevisao() {
    /* Trecho de código omitido */
    new CadastroHistoricoService().salvar(previsaoSelecionada);
}
    
02.06.2017 / 19:58