title: NullPointerException caused by not accessing database from JSP
body: <p>I am working on a project that is accessing PostgreSQL with a jdbc driver and process this data through a middleware and finally displays/updates via jsp pages. Using Eclipse I created a Dynamic Web Project. Then I have written code for all back-end and middleware operations. I can access database from static context (I mean main method of java sources). I can also use my java resources (e.g displaying) from jsp if I don't call any methods that are accessing database. But when I try to access database, it doesn't work and gives me a null pointer exception.</p>
  
  <pre><code>&lt;jsp:useBean id="user" class="dbActionManagement.UserData" scope="session"/&gt; 
  &lt;jsp:setProperty name="user" property="*"/&gt; 
  
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;next page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  You entered&lt;BR&gt;
  Name: &lt;%= user.getUsername() %&gt;&lt;BR&gt;
  Email: &lt;%= user.getEmail() %&gt;&lt;BR&gt;
  Age: &lt;%= user.getAge() %&gt;&lt;BR&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This gives me:</p>
  
  <blockquote>
    <p>SEVERE: Servlet.service() for servlet [jsp] in context with path
    [/GNYSweb] threw exception [java.lang.NullPointerException] with root
    cause java.lang.NullPointerException  at
    dbActionManagement.OfficerManager.getTotalPoint(OfficerManager.java:494)
      at dbActionManagement.UserData.getUsername(UserData.java:28)    at
    org.apache.jsp.NextPage_jsp._jspService(NextPage_jsp.java:80)     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:391)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
      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.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
      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:562)
      at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
      at
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
      at
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
      at
    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
      at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown
    Source)   at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown
    Source)   at java.lang.Thread.run(Unknown Source)</p>
  </blockquote>
  
  <p>Also java class that I am using for this is UserData and method getUserName()</p>
  
  <pre><code>public String getUsername() { 
          int a = new OfficerManager().getTotalPoint("a");
          return new Integer(a).toString(); 
  }
  </code></pre>
  
  <p>OfficerManager has a default constructor and getTotalPoint is </p>
  
  <pre><code>public int getTotalPoint(String officerId){
      int point = 0;
      ResultSet results = null;
      try {
          if(DBConnection.db.isClosed()){ // problem is caused by this line 
              DBConnection.connect(); 
          }
          Statement sql = DBConnection.db.createStatement();
                  String sqlText = "select point from officers ";
  
              sqlText += " where sicilno = '" + officerId + "'";
          results = sql.executeQuery(sqlText);
  
      } catch (SQLException e) {
          e.printStackTrace();
  
      }
      if(results != null)
          try {
              while(results.next()){
                  point = (int) results.getDouble("point");
  
              }
          } catch (SQLException e) {
              e.printStackTrace();
          }
  
      return point;
  }
  </code></pre>
  
  <p>I have added postgreSQL jdbc driver to my build path. Also I included same jdbc driver in the lib folder of Tomcat. Also I am using an instance of Tomcat v7.0 at localhost and the location of this is [workspace metadata]</p>