Nullpointerexception error while saving object

Asked

Viewed 111 times

0

I select the object through the bean and the link in the data table change in a dialog and when giving the dialog’s Ubmit I get the nullPoiterException error. Someone would 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 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)
  • You are initiating the variable previsaoSelecionada?

  • it is loaded through the Listener action inside the commandLink with the value of the list object

  • When placing an exit to see the variable it shows content.

  • Which line of null Pointer? Puts the console output

  • Provision MEDICAL VIAL VC Jun 01, 2017 3:34:59 PM javax.faces.Event.Methodexpressionactionlistener processAction GRAVE: 'java.lang.Nullpointerexception' received when calling action listening '#{searchPrevisaoBean.alterarPrevisao}' for the 'change' component'

  • line 71 register Historyservice.save(forecastsSelected);

  • Put the console output to the question. The entire output. Take advantage and put the entire Managedbean as well.

  • ready see if you can help....

  • this giving the save and the component change as you can see, but is loading the value

Show 5 more comments

1 answer

0


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

Therefore, by doing this:

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

You will receive a 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 else:

public void alterarPrevisao() {
    /* Trecho de código omitido */
    new CadastroHistoricoService().salvar(previsaoSelecionada);
}
  • when saving a history field is done , but when saving a field of the product table that is in the list the same does not occur. Knowing that a product has several hostiles. Is it possible to change this way or do I have to create a datamodel celleditable? in the case of the predicted fieldSelected.product.quantityRecent

Browser other questions tagged

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