title: javax.sql.datasource getconnection returns null
body: <p>Can someone please show me how to fix the code below so that it does not throw an error?  </p>
  
  <p>The following line of code is giving me a null pointer exception:  </p>
  
  <pre><code>return dataSource.getConnection();
  </code></pre>
  
  <p>Note that dataSource is an instance of javax.sql.DataSource which is specified in web.xml, and which works fine when called by other code.  </p>
  
  <p>Here is the actual method in DataAccessObject.java where the null pointer is occurring:  </p>
  
  <pre><code>protected static Connection getConnection(){  
    try {
       return dataSource.getConnection();  //
    } catch (SQLException e) {
       throw new RuntimeException(e);
    }
  }
  </code></pre>
  
  <p>The preceding method is being called by this line of code:  </p>
  
  <pre><code>connection = getConnection();  
  </code></pre>
  
  <p>Which is located in the following method in a class called CourseSummaryDAO as follows:  </p>
  
  <pre><code>public List&lt;CourseSummary&gt; findAll(Long sid) {
      LinkedList&lt;CourseSummary&gt; coursesummaries = new LinkedList&lt;CourseSummary&gt;();
      ResultSet rs = null;
      PreparedStatement statement = null;
      Connection connection = null;
      try {
          connection = getConnection(); //
          String sql = "select * from coursetotals where spid=?";
          statement = connection.prepareStatement(sql);
          statement.setLong(1, sid);
          rs = statement.executeQuery();
          //for every row, call read method to extract column 
          //values and place them in a coursesummary instance
          while (rs.next()) {
              CourseSummary coursesummary = read("findAll", rs);
              coursesummaries.add(coursesummary);
          }
          return coursesummaries;
       }catch (SQLException e) {
          throw new RuntimeException(e);
       } 
       finally {
          close(rs, statement, connection);
      }
   }  
  </code></pre>
  
  <p>To recreate this simply, I created the following TestCourseSummaries class:  </p>
  
  <pre><code>public class TestCourseSummaries {
      public static void main(String[] args) {
      Long id = new Long(1002);
      CourseSummaryDAO myCSDAO = new CourseSummaryDAO();
      List&lt;CourseSummary&gt; coursesummaries = myCSDAO.findAll(id);
      for(int i = 0;i&lt;coursesummaries.size();i++){
      System.out.println("type, numunits are: "+coursesummaries.get(i).getCourseType()+","+coursesummaries.get(i).getNumUnits());
      }
  }
  }  
  </code></pre>
  
  <hr>
  
  <h2>EDIT:</h2>
  
  <p>To address JustDanyul's question, I am enclosing the code that calls in my application, and the underlying DataAccessObject code which is extended by the two DAO objects in the calling code:  </p>
  
  <p>Here is the code in my application which triggers the error.  See there are two classes that each extended DataAccessObject.  Perhaps they are conflicting with each other, causing the second one not to get the database connection?  </p>
  
  <pre><code>protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
  throws ServletException, IOException {  
     String idString = req.getParameter("id");
     Long id = new Long(idString);
     ThisObj thisobj = new ThisDAO().find(id);
     req.setAttribute("thisobj", thisobj);
     ThoseObjDAO myThoseDAO = new ThoseObjDAO();
     List&lt;ThoseObj&gt; thoseobjects = myThoseObjDAO.findAll(id);
     req.setAttribute("thoseobjects", thoseobjects);
     jsp.forward(req, resp);
  </code></pre>
  
  <p>}</p>
  
  <p>And here is the code for the DataAccessObject class which is extended by the two DAO classes in the calling code:  </p>
  
  <pre><code>public class DataAccessObject {
  private static DataSource dataSource;
  private static Object idLock = new Object();
  
  public static void setDataSource(DataSource dataSource) {
     DataAccessObject.dataSource = dataSource;
  }
  
  protected static Connection getConnection() {
     try {return dataSource.getConnection();}
     catch (SQLException e) {throw new RuntimeException(e);}
  }
  
  protected static void close(Statement statement, Connection connection) {
     close(null, statement, connection);
  }
  
  protected static void close(ResultSet rs, Statement statement, Connection connection) {
     try {
        if (rs != null) rs.close();
        if (statement != null) statement.close();
        if (connection != null) connection.close();
     } catch (SQLException e) {throw new RuntimeException(e);}
  }
  
  protected static Long getUniqueId() {
     ResultSet rs = null;
     PreparedStatement statement = null;
     Connection connection = null;
     try {
        connection = getConnection();
        synchronized (idLock) {
           statement = connection.prepareStatement("select next_value from sequence");
           rs = statement.executeQuery();
           rs.first();
           long id = rs.getLong(1);
           statement.close();
           statement = connection.prepareStatement("update sequence set next_value = ?");
           statement.setLong(1, id + 1);
           statement.executeUpdate();
           statement.close();
           return new Long(id);
        }
     }
     catch (SQLException e) {throw new RuntimeException(e);}
     finally{close(rs, statement, connection);}
  }
  }  
  </code></pre>
  
  <p>The data source is created in web.xml, as follows:  </p>
  
  <pre><code>&lt;resource-ref&gt;
      &lt;description&gt;dataSource&lt;/description&gt;
      &lt;res-ref-name&gt;datasource&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>