6
Good afternoon guys. I am developing a Java test application with JPA, CDI and Tomcat. I created a basic test class and am getting the following error.
Erro: Exception in thread "main" java.lang.NullPointerException
at br.com.hcancerbarretos.espec.dao.EspecialidadesDAO.getEspecialidades(EspecialidadesDAO.java:16)
at br.com.hcancerbarretos.espec.testes.Teste.main(Teste.java:13)
To EspecialidadeDAO
is null, is not being injected correctly with the @Inject
. The strange thing is that if I create a JSF page and display the values in a datatable, all that has @Inject
is injected normally.
I also created a Webservice class called Specialties, where the Specialty Dependency injection does not work.
Can anyone tell me how to fix it? Why? Any output? Below are the files and classes.
Model class:
import java.io.Serializable;
import javax.persistence.*;
import java.util.Date;
@Entity
@NamedQuery(name="Especialidade.findAll", query="SELECT e FROM Especialidade e")
public class Especialidade implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name="ESPECEALIDADE_ID")
private long especialidadeId;
private String descricao;
@Temporal(TemporalType.DATE)
@Column(name="DT_ALT")
private Date dtAlt;
@Temporal(TemporalType.DATE)
@Column(name="DT_INCL")
private Date dtIncl;
private String sigla;
@Column(name="USU_ALT")
private String usuAlt;
@Column(name="USU_INCL")
private String usuIncl;
public Especialidade() {
}
//getters and setters ...
}
Class code Speciality.
import java.util.List;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import br.com.hcancerbarretos.espec.model.Especialidade;
public class EspecialidadesDAO {
@Inject
private EntityManager em;
public List<Especialidade> getEspecialidades() {
return em.createNamedQuery("Especialidade.findAll", Especialidade.class).getResultList();
}
public String getEspecialidadePorCodigo(long codigo) {
return em.createQuery("select e.descricao From Especialidade e Where e.especialidadeId = :codigo", String.class)
.setParameter("codigo", codigo).getSingleResult();
}
}
Class EntityManagerProducer
:
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Disposes;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
@ApplicationScoped
public class EntityManagerProducer {
private EntityManagerFactory factory;
public EntityManagerProducer() {
this.factory = Persistence.createEntityManagerFactory("Especialidade");
}
@Produces
@RequestScoped
public EntityManager createEntityManager() {
return factory.createEntityManager();
}
public void closeEntityManager(@Disposes EntityManager manager) {
manager.close();
}
}
Test class:
import java.util.List;
import br.com.hcancerbarretos.espec.dao.EspecialidadesDAO;
import br.com.hcancerbarretos.espec.model.Especialidade;
public class Teste {
public static void main(String[] args) {
EspecialidadesDAO dao = new EspecialidadesDAO();
List<Especialidade> especialidades = dao.getEspecialidades();
}
}
Class Especialidadews.
import javax.inject.Inject;
import br.com.hcancerbarretos.espec.dao.EspecialidadesDAO;
public class EspecialidadeWS {
@Inject
private EspecialidadesDAO dao;
public String getEspecialidade(long codigo){
System.out.println("DAO " + dao);
return dao.getEspecialidadePorCodigo(codigo);
}
}
Filing cabinet web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>Especialidade</display-name>
<welcome-file-list>
<welcome-file>index.jsf</welcome-file>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.jboss.weld.environment.servlet.Listener</listener-class>
</listener>
<resource-env-ref>
<resource-env-ref-name>BeanManager</resource-env-ref-name>
<resource-env-ref-type>javax.enterprise.inject.spi.BeanManager</resource-env-ref-type>
</resource-env-ref>
</web-app>
Filing cabinet META-INF\context.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<!-- disables storage of sessions across restarts -->
<Manager pathname="" />
<Resource name="BeanManager" auth="Container"
type="javax.enterprise.inject.spi.BeanManager" factory="org.jboss.weld.resources.ManagerObjectFactory" />
</Context>
The archive beans.xml
is created, but empty, as recommended.
If the exception is occurring in line 16 of the Specialties, then it must have been injected, and the error may be in it. Could also paste the code of Specialties?
– Victor T.
Debugging the application I check that the Entitymanager was not injected. I put the code of the class just below Victor. Thanks.
– Reginaldo