An error occurred while performing resource injection on the managed bean paisControler

Asked

Viewed 3,136 times

-1

Trying to start a system page shows this error.

I’ll put the codes:

DAO

package br.com.netsoft.desif.dao.endereco;

import java.io.Serializable;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import br.com.netsoft.desif.model.endereco.PaisEntity;

public class PaisDao implements Serializable {

    private static final long serialVersionUID = 1402488530698938353L;

    final private String hql = " p FROM PaisEntity p ";

    EntityManager em;

    public List<PaisEntity> listarPaises() {

        StringBuilder hqlSelect = new StringBuilder(hql);

        final String query = "SELECT " + hqlSelect;

        Query querySelect = em.createQuery(query);

        @SuppressWarnings("unchecked")
        List<PaisEntity> resultList = querySelect.getResultList();

        return resultList;

    }

}

ENTITY

package br.com.netsoft.desif.model.endereco;

import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;

public class PaisEntity implements Serializable {

    private static final long serialVersionUID = 7784827196506672790L;
    private Long id;
    private String descricao;

    @Id
    @Column(name = "pai_id")
    @SequenceGenerator(name = "enderecos.end_pais_pai_id_seq", sequenceName = "enderecos.end_pais_pai_id_seq", allocationSize = 1)
    @GeneratedValue(generator = "enderecos.end_pais_pai_id_seq", strategy = GenerationType.SEQUENCE)
    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    @Column(name = "pai_descricao")
    public String getDescricao() {
        return descricao;
    }

    public void setDescricao(String descricao) {
        this.descricao = descricao;
    }

}

CONTROLLER

package br.com.netsoft.desif.controler.endereco;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.faces.bean.ManagedBean;

import br.com.netsoft.desif.dao.endereco.PaisDao;
import br.com.netsoft.desif.model.endereco.PaisEntity;

@ManagedBean(name = "paisControler")
@RequestScoped
public class PaisControler extends PaisEntity {

    private static final long serialVersionUID = -8396738159447021720L;

    private List<PaisEntity> paises;

    private PaisDao paisDao;

    public List<PaisEntity> getPaises() {
        if (paises == null) {
            return new ArrayList<PaisEntity>();
        }
        return paises;
    }

    public void setPaises(List<PaisEntity> paises) {
        this.paises = paises;
    }

    @PostConstruct
    public void init() {
        System.out.println("Página inicial de países.");
        @SuppressWarnings("unused")
        List<PaisEntity> paisesBusca = paisDao.listarPaises();

        ArrayList<PaisEntity> paises = (ArrayList<PaisEntity>) paisDao.listarPaises();

        for (Iterator<?> iterator = paises.iterator(); iterator.hasNext();) {
            PaisEntity pais = (PaisEntity) iterator.next();
            pais.setId(pais.getId());
            pais.setDescricao(pais.getDescricao());
            paises.add(pais);
        }

        this.setPaises(paises);
    }

    public String getMensagem() {
        return "Teste";
    }

}

VISION

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
        xmlns:ui="http://java.sun.com/jsf/facelets"
        xmlns:f="http://java.sun.com/jsf/core"
        xmlns:h="http://java.sun.com/jsf/html">

    <h:head>
        <title>Hello JSF!</title>
    </h:head>
    <h:body>
          #{paisControler.mensagem}

        <h:dataTable var="pais" value="#{paisControler.paises}">

            <h:column headerText="Id">
                <h:outputText value="#{pais.id}" />
            </h:column>

            <h:column headerText="Year">
                <h:outputText value="#{pais.descricao}" />
            </h:column>

        </h:dataTable>

    </h:body>

    </html>

Error:

mar 21, 2017 8:08:40 AM com.sun.faces.application.view.FaceletViewHandlingStrategy handleRenderException
GRAVE: Error Rendering View[/public/index.xhtml]
javax.el.ELException: /public/index.xhtml: Um erro ocorreu ao realizar a injeção de recurso no bean gerenciado paisControler
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:90)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    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:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    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:230)
    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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    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:341)
    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:1441)
    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)
Caused by: com.sun.faces.mgbean.ManagedBeanCreationException: Um erro ocorreu ao realizar a injeção de recurso no bean gerenciado paisControler
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:227)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:94)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:137)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:150)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
    ... 33 more
Caused by: com.sun.faces.spi.InjectionProviderException
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:115)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:95)
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221)
    ... 47 more
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:113)
    ... 49 more
Caused by: java.lang.NullPointerException
    at br.com.netsoft.desif.controler.endereco.PaisControler.init(PaisControler.java:39)
    ... 54 more

mar 21, 2017 8:08:40 AM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [Faces Servlet] in context with path [/desif] threw exception [Um erro ocorreu ao realizar a injeção de recurso no bean gerenciado paisControler] with root cause
java.lang.NullPointerException
    at br.com.netsoft.desif.controler.endereco.PaisControler.init(PaisControler.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokeAnnotatedMethod(WebContainerInjectionProvider.java:113)
    at com.sun.faces.vendor.WebContainerInjectionProvider.invokePostConstruct(WebContainerInjectionProvider.java:95)
    at com.sun.faces.mgbean.BeanBuilder.invokePostConstruct(BeanBuilder.java:221)
    at com.sun.faces.mgbean.BeanBuilder.build(BeanBuilder.java:103)
    at com.sun.faces.mgbean.BeanManager.createAndPush(BeanManager.java:409)
    at com.sun.faces.mgbean.BeanManager.create(BeanManager.java:269)
    at com.sun.faces.el.ManagedBeanELResolver.resolveBean(ManagedBeanELResolver.java:244)
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:116)
    at com.sun.faces.el.DemuxCompositeELResolver._getValue(DemuxCompositeELResolver.java:176)
    at com.sun.faces.el.DemuxCompositeELResolver.getValue(DemuxCompositeELResolver.java:203)
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:94)
    at org.apache.el.parser.AstValue.getValue(AstValue.java:137)
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:184)
    at org.jboss.weld.el.WeldValueExpression.getValue(WeldValueExpression.java:50)
    at com.sun.faces.facelets.el.ELText$ELTextVariable.writeText(ELText.java:227)
    at com.sun.faces.facelets.el.ELText$ELTextComposite.writeText(ELText.java:150)
    at com.sun.faces.facelets.compiler.TextInstruction.write(TextInstruction.java:85)
    at com.sun.faces.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:82)
    at com.sun.faces.facelets.compiler.UILeaf.encodeAll(UILeaf.java:183)
    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:443)
    at com.sun.faces.application.view.MultiViewHandler.renderView(MultiViewHandler.java:131)
    at javax.faces.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:337)
    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:230)
    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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:478)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
    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:341)
    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:1441)
    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)
  • @Guilherme request the merge between the two accounts in this form, so that you can edit and comment on this question.

1 answer

0

The log is already pointing out the error:

java.lang.NullPointerException
 at br.com.netsoft.desif.controler.endereco.PaisControler.init(PaisControler.java:39)

This NPE occurs in the following code snippet:

List<PaisEntity> paisesBusca = paisDao.listarPaises();

The problem is because you didn’t inject the Father, so it is null here and so the call of your method listarPaises causes NPE. Add the injection of this attribute into the Mbean and the problem will be solved.

  • Can you give me an example ?

  • You are using Spring or CDI in your project?

  • CDI <! -- CDI --> <dependency> <groupid>javax.Enterprise</groupid> <artifactId>cdi-api</artifactId> <version>1.2</version> <Scope>provided</Scope> </dependency>

  • Then just put a @Inject in your.

  • I switched to, but it didn’t work. public class Paiscontroler extends Paisentity { private Static final long serialVersionUID = -8396738159447021720L; private List<Paisentity> countries; @Inject Paisdao paisDao;

  • Just to test, call the Dao constructor directly to see if the problem is with @Inject.

  • This works, but then with the debug the error is in the DAO. @Postconstruct public void init() { System.out.println("Home page of countries."); paisDao = new Paisdao();

  • Then the error happens on this line: Query querySelect = in.createQuery(query);

  • I know it’s almost the same idea, but after researching and making some changes, I couldn’t evolve.

  • The error is the call of the listPaises() because the Dotto is null. You only need to initialize it, either by constructor or by dependency injection.

  • I did so: @Persistencecontext Entitymanager on; .

  • Something else I can do ??

  • Start entityManager without dependency injection to see if it works. You would call Persistence.createEntityManagerFactory and then createEntityManager to initialize in.

  • I did so: Entitymanager in; public List<Paisentity> listPaises() { Stringbuilder hqlSelect = new Stringbuilder(hql); final String query = "SELECT" + hqlSelect; Entitymanagerfactory Factory = Persistence.createEntityManagerFactory("Paisentity"); Entitymanager manager = Factory.createEntityManager(); Query querySelect = manager.createQuery(query); @Suppresswarnings("unchecked") List<Paisentity> resultList = querySelect.getResultList(); Return resultList; }

  • Gave this error: Caused by: javax.persistence.Persistenceexception: No Persistence Provider for Entitymanager named Paisentity at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54) at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:32) at br.com.netsoft.desif.dao.endereco.Paisdao.listarPaises(Paisdao.java:28) at br.com.netsoft.desif.controler.endereco.Paiscontroler.init(Paiscontroler.java:43) Acho.

Show 10 more comments

Browser other questions tagged

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