我有這個問題,它的2天我不明白爲什麼會發生。 我想驗證登錄。我的代碼:(持久性)無法創建實體管理器
public String Verify(String name, String password) {
String admin="";
Query q = emf
.createEntityManager()
.createQuery(
"SELECT u FROM user u where u.name = :name and u.password=:password");
q.setParameter("name", name);
q.setParameter("password", password);
User u = (User) q.getSingleResult();
if (u.getAdmin().equalsIgnoreCase("yes")) {
admin = "yes";
} else {
admin = "no";
}
return admin;
}
我再收到此錯誤:
java.lang.NullPointerException
at db.DbProcessor.Verify(DbProcessor.java:19)
at controller.VerifikoServlet.doGet(VerifikoServlet.java:25)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
第19行是 「.createEntityManager()」
**調試**和** **記錄總是有幫助的 - 否則你'EntityManagerFactory'可能不被初始化。即使你可以拆分你的代碼,那麼你會看到哪一步是空的。 – gaborsch
我在哪裏以及如何「emf」我猜是一個EntityManagerFactory實例?看起來它可能是空的......你是否正在使用一些依賴注入? –
它在這種情況下幫助不大:)但是我怎麼能初始化呢? –