尝试在数据库中查找实体
我有这样的代码:
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)
I have a code like this:
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");
}
I am trying to find an entity in database, if the entity is in the database then it will perform an action, if it does not it will perform another action.
I might have a logical mistake in trying first find and then contains method.. but I don't know.
Thanks in advance for your help.
EDIT:
Sorry for the late reply. I had to deal with an urgent matter. Full error stack trace is:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需检查 EntityManager.find(...) 方法。
如果数据库中不存在该实体,它将返回
null
。因此,这取决于AJPAController.find(...)
方法的实现,它如何处理该信息。 (只是为了澄清:据我了解,AJPAController
是EntityManager
的包装器。)Just check the EntityManager.find(...) method.
It will return
null
if the entity does not exist in database. So it's up to the implementation of theAJPAController.find(...)
method what it is doing with that information. (Just for clarification: As I understood,AJPAController
is a wrapper around anEntityManager
.)您必须向这篇文章“AJPAController”代码添加完整的错误堆栈跟踪。没有这个,我们无法帮助您。
也许 Integer.parseInt 或 request.getParameter 返回 nullpointerException?
You must add to this post "AJPAController" code an full error stacktrace. Without this, we can't help you.
Maybe Integer.parseInt, or request.getParameter return nullpointerexception?