Good afternoon, guys. I'm developing a Java test application with JPA, CDI and Tomcat. I created a basic test class and I'm 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)
The EspecialidadeDAO
is null, not being injected correctly with @Inject
. The strangest thing is that if I create a JSF page and display the values in a dataTable, everything that has @Inject
is injected normally.
I've also created a Webservice class called WSP Specialty, where the ODBC injection injection does not work.
Does anyone know how to fix this? Why? Any way out? Below are the files and classes.
Template 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 ...
}
Code of the SpecialTourism class.
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();
}
}
WS Special Class.
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);
}
}
File 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>
File 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 beans.xml
file is created, but empty, as recommended.