title: @Resource annotation for JNDI datasource injection in tomat 7
body: <p>I am working with a project with accesses a <code>JNDI</code> datasource in tomcat 7. Here is my META-INF/context.xml files:</p>
  
  <pre><code>   &lt;?xml version="1.0" encoding="UTF-8"?&gt;
     &lt;Context&gt;
  
      &lt;Resource name="jdbc/ambes" auth="Container" type="javax.sql.DataSource"
             maxActive="50" maxIdle="30" maxWait="10000"
             username="root" password="" 
             driverClassName="com.mysql.jdbc.Driver"
             url="jdbc:mysql://localhost:3306/ANTTestDb"/&gt;
  
     &lt;/Context&gt;
  </code></pre>
  
  <p>This is my dao class, the dataSource is successfully injected to my class via context lookup service as you see below:</p>
  
  <pre><code>  public class CoffeeDataBase {
  
    private DataSource dataSource;
  
    public CoffeeDataBase() {
  
      try {
  
          Context ctx = new InitialContext();
          dataSource = (DataSource) ctx.lookup("java:comp/env/jdbc/ambes");
  
          } catch (Exception e) {
          e.printStackTrace();
      }
  
     // other methods definition
  
  }
  </code></pre>
  
  <p>but when i replace the lookup service with @Resourse anotaion  like this:</p>
  
  <pre><code>  @Resource(lookup="java:comp/env/jdbc/ambes")
    public class CoffeeDataBase {
  
    private DataSource dataSource;
  
    public CoffeeDataBase() {
    }
  
     // other methods definition
  
  }
  </code></pre>
  
  <p>i am getting this NullPointerExeption like this:</p>
  
  <pre><code>java.lang.NullPointerException
      at com.ambestephen.dao.CoffeeDataBase.getCoffeeList(CoffeeDataBase.java:63)
      at com.ambestephen.controller.ShowCoffees.doGet(ShowCoffees.java:16)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
      at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
      at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
      at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:51)
      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:222)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
      at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
      at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
      at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:953)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1041)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:603)
      at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at java.lang.Thread.run(Unknown Source)
  </code></pre>
  
  <p>I have also tried <code>@Resource(name="jdbc/ambes")</code> but same exception!</p>