我是靠谱客的博主 忧虑胡萝卜,最近开发中收集的这篇文章主要介绍java如何抓取空指针异常_在tomcat 7中获取空指针异常@Resource注释,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

这是context.xml中的Resource元素::

type="javax.sql.DataSource" driverClassName="oracle.jdbc.driver.OracleDriver"

url="jdbc:oracle:thin:@localhost:1521:XE"

username="hr" password="hr" maxActive="20" maxIdle="10"

maxWait="-1"/>

这是我在web.xml中的参考资源:

Oracle Datasource

jdbc/myoracle

javax.sql.DataSource

Container

然后@Resource在代码中使用注释:

@Resource(mappedName = "jdbc/myoracle")

DataSource ds;

当我尝试在ds上使用getConnection()创建连接对象时,在运行时出现空指针异常:

Oct 24, 2011 12:18:21 PM org.apache.catalina.core.StandardWrapperValve invoke

INFO: java.lang.NullPointerException

at jdbc.patientDaoImpl.get_patients(patientDaoImpl.java:248)

at org.apache.jsp.index2_jsp._jspService(index2_jsp.java:92)

at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)

at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)

at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)

at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)

at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:223)

at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)

at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)

at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)

at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:563)

at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:399)

at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:317)

at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:204)

at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:311)

at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)

at java.lang.Thread.run(Thread.java:662)

但是,如果我使用initialContext查找,该应用程序将正常运行。

Context initContext = new InitialContext();

Context envContext = (Context)initContext.lookup("java:/comp/env");

DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");

我要去哪里错了?

最后

以上就是忧虑胡萝卜为你收集整理的java如何抓取空指针异常_在tomcat 7中获取空指针异常@Resource注释的全部内容,希望文章能够帮你解决java如何抓取空指针异常_在tomcat 7中获取空指针异常@Resource注释所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(60)

评论列表共有 0 条评论

立即
投稿
返回
顶部