title: Get Data from DB when use JSP
body: <p>I have a problem with my project</p>
  
  <p>My DBConnect</p>
  
  <pre><code>package shop.connect;
  
  import java.sql.Connection;
  import java.sql.DriverManager;
  
  public class DBConnect {
      public static Connection getConnection() {
          Connection connection = null;
          try {
              Class.forName("com.mysql.jdbc.Driver");
              connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/shop", "root", "123456789");
              System.out.println("Connect succesful");
          } catch (Exception e) {
              // TODO: handle exception
          }
          return connection;
      }
  
      public static void main(String[] args) {
          System.out.println(getConnection());
      }
  }
  </code></pre>
  
  <p>I have 2 class</p>
  
  <pre><code>public class Category {
      private long categoryID;
      private String categoryName;
  </code></pre>
  
  <p>And second class CategoryDAO</p>
  
  <pre><code>public class CategoryDAO {
  
  // get danh sach the loai
  public ArrayList&lt;Category&gt; getListCategory() throws SQLException {
      Connection connection = DBConnect.getConnection();
      String sql = "SELECT * from category";
      PreparedStatement pStatement = connection.prepareCall(sql);
      ResultSet rSet = pStatement.executeQuery();
      ArrayList&lt;Category&gt; list = new ArrayList&lt;&gt;();
      while (rSet.next()) {
          Category category = new Category();
          category.setCategoryID(rSet.getInt("category_id"));
          category.setCategoryName(rSet.getString("category_name"));
          list.add(category);
      }
      return list;
  }
  
  public static void main(String[] args) throws SQLException {
      CategoryDAO dao = new CategoryDAO();
      for (Category ds : dao.getListCategory()) {
          System.out.println(ds.getCategoryID() + " - " + ds.getCategoryName());
      }
  }
  </code></pre>
  
  <p>}
  And my JSP</p>
  
  <pre><code>&lt;%
  CategoryDAO categoryDAO = new CategoryDAO();
  for (Category c : categoryDAO.getListCategory()) {%&gt;
  &lt;li&gt;&lt;a href="products.html"&gt;&lt;%=c.getCategoryName()%&gt;&lt;/a&gt;&lt;/li&gt;
  &lt;%}%&gt;
  </code></pre>
  
  <p>When i run getListCategory's class, data display corret.
  But when i try to display it in JSP, NullPointException appers.
  When i use Neatbean, this JSP work fine. But with Eclipse, this bug appears. 
  Sorry about my English but who can help me fix this bug.
  I'm newbie :(</p>
  
  <pre><code>Type Exception Report
  
  Message An exception occurred processing JSP page /header.jsp at line 97
  
  Description The server encountered an unexpected condition that prevented it from fulfilling the request.
  
  Exception
  org.apache.jasper.JasperException: An exception occurred processing JSP page /header.jsp at line 97
  
  94:                         &lt;li&gt;&lt;a href="#"&gt;Laptops &amp; Notebooks&lt;/a&gt;
  95:                             &lt;ul class="drop"&gt;
  96:                                 &lt;%
  97:                                     for( Category c: categoryDAO.getListCategory()){
  98:                                 %&gt;
  99:                                 &lt;li&gt;&lt;a href="products.html"&gt;&lt;%=c.getCategoryName()%&gt;&lt;/a&gt;&lt;/li&gt;
  100:                                &lt;%
  
  
  Stacktrace:
      org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:588)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:481)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  
  
  Root Cause
  java.lang.NullPointerException
      shop.dao.CategoryDAO.getListCategory(CategoryDAO.java:18)
      org.apache.jsp.header_jsp._jspService(header_jsp.java:215)
      org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:443)
      org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
      org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
      org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
  </code></pre>