我有这样的代码:
AJPAController aJPAController;
AClass aClass = aJPAController.find((Integer.parseInt(request.getParameter("id")));
try{
if(aJpaController.contains(aClass)){
response.sendRedirect("gosomewhere.com");
}
}
catch (java.lang.NullPointerException R){
response.sendRedirect("gosomewhereelse.com");
}
我试图在数据库中找到一个实体,如果该实体在数据库中,那么它将执行一个动作,如果没有,它将执行另一个动作。
在尝试先查找然后包含方法时,我可能会遇到逻辑错误。但是我不知道。
在此先感谢您的帮助。
编辑:
这么晚才回复很抱歉。我不得不处理一件紧急的事情。完整的错误堆栈跟踪为:
Description: An unhandled exception occurred during the execution of the web application. Please review the following stack trace for more information regarding the error.
Exception Details: java.lang.NullPointerException
null
Possible Source of Error:
Class Name: org.apache.jsp.user.report_005fredirect_jsp
File Name: report_005fredirect_jsp.java from
Method Name: _jspService
Line Number: 74
Source not available. Information regarding the location of the exception can be identified using the exception stack trace below.
Stack Trace:
org.apache.jsp.user.report_005fredirect_jsp._jspService(report_005fredirect_jsp.java from :74)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:408)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:483)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:373)
javax.servlet.http.HttpServlet.service(HttpServlet.java:847)
org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1523)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:343)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
com.labristeknoloji.lcas.client.filters.SingleSignOutNameAddFilter.doFilter(SingleSignOutNameAddFilter.java:56)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.jasig.cas.client.util.AssertionThreadLocalFilter.doFilter(AssertionThreadLocalFilter.java:40)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.jasig.cas.client.util.HttpServletRequestWrapperFilter.doFilter(HttpServletRequestWrapperFilter.java:50)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.jasig.cas.client.validation.AbstractTicketValidationFilter.doFilter(AbstractTicketValidationFilter.java:167)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
com.labristeknoloji.lcas.client.filters.LCASAuthenticationFilter.doFilter(LCASAuthenticationFilter.java:111)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:104)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
com.labristeknoloji.lrms.web.filter.LocaleFilter.doFilter(LocaleFilter.java:76)
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:256)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:215)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:277)
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:188)
org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:641)
com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:97)
com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:85)
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:185)
org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:332)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:233)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:229)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:334)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:814)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:743)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:936)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:682)
java.lang.Thread.run(Thread.java:619)
最佳答案
只需检查EntityManager.find(...)方法。
如果实体在数据库中不存在,它将返回null
。因此,AJPAController.find(...)
方法的实现取决于该信息的处理方式。 (为澄清起见:据我了解,AJPAController
是EntityManager
的包装。)
关于java - 尝试在数据库中查找实体,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6608203/