title: Getting null pointer exception @Resource annotation in tomcat 7
body: <p>Here is my Resource element in context.xml :: </p>
  
  <pre><code>&lt;Resource name="jdbc/myoracle" auth="Container"
                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"/&gt; 
  </code></pre>
  
  <p>Here is my Resource reference in web.xml:</p>
  
  <pre><code>       &lt;resource-ref&gt;
              &lt;description&gt;Oracle Datasource&lt;/description&gt;
              &lt;res-ref-name&gt;jdbc/myoracle&lt;/res-ref-name&gt;
              &lt;res-type&gt;javax.sql.DataSource&lt;/res-type&gt;
              &lt;res-auth&gt;Container&lt;/res-auth&gt;
          &lt;/resource-ref&gt;
  </code></pre>
  
  <p>I then use <code>@Resource</code> annotation in my code:</p>
  
  <pre><code>      @Resource(mappedName = "jdbc/myoracle")
          DataSource ds;
  </code></pre>
  
  <p>When I try to create connection object using getConnection() on ds, I get a null pointer exception during runtime:</p>
  
  <pre><code>    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)
  </code></pre>
  
  <p>But if I lookup using initialContext the app works fine.</p>
  
  <pre><code>    Context initContext = new InitialContext();
      Context envContext  = (Context)initContext.lookup("java:/comp/env");
      DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle");
  </code></pre>
  
  <p>Where am I going wrong?</p>