Estou com um problema que é que quando tento usar o método "existe" do usuarioDao, occore um null pointer exception. Pareçe que a entity manager não foi inicializada ou algo do tipo, mas a príncipio pareçe estar tudo certo. Não sei onde esta o erro.
Aqui está o código da minha classe usuarioDao:
package br.com.caelum.vraptor.dao;
import javax.persistence.EntityManager;
import br.com.caelum.vraptor.model.Usuario;
public class UsuarioDao {
private final EntityManager em;
// @Inject
public UsuarioDao(EntityManager em) {
this.em = em;
}
@Deprecated
public UsuarioDao() {
this(null); // para uso do CDI
}
public boolean existe(Usuario usuario) {
return !em.createQuery("select u from Usuario u where u.nome = "
+ ":nome and u.senha = :senha", Usuario.class)
.setParameter("nome", usuario.getNome())
.setParameter("senha", usuario.getSenha())
.getResultList().isEmpty();
}
public void salva(Usuario usuario) {
em.persist(usuario);
}
}
E da classe EntityManagerProducer:
package br.com.caelum.vraptor.producers;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import br.com.caelum.vraptor.util.JPAUtil;
public class EntityManagerProducer {
@Produces @RequestScoped
public EntityManager criaEntityManager(){
return JPAUtil.criaEntityManager();
}
}
E o Stacktrace do erro, caso seja util:
Apr 06, 2015 8:28:05 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [default] in context with path [/vraptor-produtos] threw exception [net.vidageek.mirror.exception.ReflectionProviderException: Could not invoke method autentica] with root cause java.lang.NullPointerException at br.com.caelum.vraptor.dao.UsuarioDao.existe(UsuarioDao.java:22) at br.com.caelum.vraptor.controller.LoginController.autentica(LoginController.java:40) at br.com.caelum.vraptor.controller.LoginController$Proxy$$$_WeldClientProxy.autentica(Unknown Source) 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 net.vidageek.mirror.provider.java.PureJavaMethodReflectionProvider.invoke(PureJavaMethodReflectionProvider.java:38) at net.vidageek.mirror.invoke.MethodHandlerByMethod.withArgs(MethodHandlerByMethod.java:54) at br.com.caelum.vraptor.observer.ExecuteMethod.execute(ExecuteMethod.java:87) 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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253) at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232) at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169) at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165) at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112) at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:78) at br.com.caelum.vraptor.interceptor.FlashInterceptor.intercept(FlashInterceptor.java:98) at br.com.caelum.vraptor.interceptor.FlashInterceptor$Proxy$$$WeldClientProxy.intercept(Unknown Source) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83) at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor.intercept(ExceptionHandlerInterceptor.java:75) at br.com.caelum.vraptor.interceptor.ExceptionHandlerInterceptor$Proxy$$$WeldClientProxy.intercept(Unknown Source) at br.com.caelum.vraptor.core.ToInstantiateInterceptorHandler.execute(ToInstantiateInterceptorHandler.java:58) at br.com.caelum.vraptor.core.DefaultInterceptorStack.next(DefaultInterceptorStack.java:83) at br.com.caelum.vraptor.core.DefaultInterceptorStack.start(DefaultInterceptorStack.java:93) at br.com.caelum.vraptor.core.DefaultInterceptorStack$Proxy$$$_WeldClientProxy.start(Unknown Source) at br.com.caelum.vraptor.observer.RequestHandlerObserver.handle(RequestHandlerObserver.java:86) 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 org.jboss.weld.injection.MethodInjectionPoint.invokeOnInstanceWithSpecialValue(MethodInjectionPoint.java:93) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:266) at org.jboss.weld.event.ObserverMethodImpl.sendEvent(ObserverMethodImpl.java:253) at org.jboss.weld.event.ObserverMethodImpl.notify(ObserverMethodImpl.java:232) at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:169) at org.jboss.weld.event.ObserverNotifier.notifyObserver(ObserverNotifier.java:165) at org.jboss.weld.event.ObserverNotifier.notifyObservers(ObserverNotifier.java:119) at org.jboss.weld.event.ObserverNotifier.fireEvent(ObserverNotifier.java:112) at org.jboss.weld.event.EventImpl.fire(EventImpl.java:83) at br.com.caelum.vraptor.VRaptor.doFilter(VRaptor.java:116) at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239) at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:501) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79) at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:516) at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1086) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:659) at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1558) at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1515) 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)