这是我的Servlet
//this is the url servlet
@WebServlet("/CRUDServlet")
public class CRUDServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
//this gives me problem
@EJB
private CRUD crud;
public CRUDServlet() {
super();
// TODO Auto-generated constructor stub
}
// GET METHOD
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//The problem is here because when I call create the program gives me Null Pointer
crud.create(new Home("Parigi", 100,00));
response.getWriter().println("Hello friend!");
}
}
Bean是无状态的会话bean:
@Stateless
public class CRUDBean implements CRUD {
@PersistenceContext(name="crudPersistence")
private EntityManager em;
public CRUDBean() {}
@Override
public void create(OggettoDaVendere o) {
em.persist(o);
return;
}
...
}
,当我转到本地URL:http://localhost:8080/Example/CRUDServlet
时,它给了我空指针异常,我不知道为什么。有人可以帮助我吗?
lug 11,2018 4:32:09 PM org.apache.catalina.core.StandardWrapperValve 为Servlet [servlet.CRUDServlet]中的调用GRAVE:Servlet.service() 路径[/ EsercizioCRUD]的上下文引发异常 java.lang.NullPointerException在 servlet.CRUDServlet.doGet(CRUDServlet.java:46)在 javax.servlet.http.HttpServlet.service(HttpServlet.java:635)在 javax.servlet.http.HttpServlet.service(HttpServlet.java:742)在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96) 在 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:493) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) 在 org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:650) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342) 在 org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:800) 在 org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) 在 org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:800) 在 org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1471) 在 org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) 在 java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:624) 在 org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61) 在java.lang.Thread.run(Thread.java:748)