Note that there are some explanatory texts on larger screens.

plurals
  1. PONullPointerException when using DAO class
    text
    copied!<p>I tried out DAO Pattern after reading <a href="http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html" rel="nofollow">http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html</a> but I get NullPointerException when I run the servlet</p> <p><strong>DAOFactory</strong></p> <pre><code>package dao; import java.sql.SQLException; public abstract class DAOFactory { // List of DAO types supported by the factory public static final int MYSQL = 1; public abstract UserDAO getUserDAO() throws SQLException; public static DAOFactory getDAOFactory(int whichFactory) { switch (whichFactory) { case MYSQL: return new MySQLDAOFactory(); default : return null; } } } </code></pre> <p><strong>MySQLDAOFactory</strong></p> <pre><code>package dao; import java.sql.*; public class MySQLDAOFactory extends DAOFactory { public MySQLDAOFactory() { } public static final String DRIVER="com.mysql.jdbc.Driver"; public static final String DBURL="jdbc:mysql://localhost:3306/musicompany"; public static Connection createConnection() { Connection conn=null; try{ Class.forName(DRIVER); conn = DriverManager.getConnection(DBURL,"root","toor"); }catch(Exception e){} return conn; } public UserDAO getUserDAO() throws SQLException{ return new MySQLUserDAO(); } } </code></pre> <p><strong>MySQLUserDAO</strong></p> <pre><code>package dao; import java.sql.*; import java.util.ArrayList; import model.User; public class MySQLUserDAO implements UserDAO { Connection conn=null; Statement s=null; public MySQLUserDAO() throws SQLException{ conn = MySQLDAOFactory.createConnection(); s = conn.createStatement(); } public int insertUser(String fname, String lname) { try{ //code to insertUser }catch(Exception e){} return key; } public ArrayList&lt;User&gt; selectUsers() { ResultSet rs=null; ArrayList customerList=null; try{ rs =s.executeQuery("select * from users"); customerList = new ArrayList&lt;User&gt;(); while(rs.next()) { customerList.add(new User(rs.getString("name"), rs.getString("pass"), rs.getInt("type"), rs.getString("email"))); } return customerList; }catch(Exception e){} return customerList; } } </code></pre> <p><strong>UserDAO</strong></p> <pre><code>package dao; import java.util.ArrayList; import model.User; public interface UserDAO { public int insertUser(String fname, String lname); public ArrayList&lt;User&gt; selectUsers(); } </code></pre> <p><strong>model.User.java</strong></p> <p>This class has the following fields and the corresponding accessor methods.</p> <pre><code>String name; String pass; short type; String email; </code></pre> <p><strong>My Servlet file</strong></p> <pre><code>public class AddRetrieve extends HttpServlet { protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { // create the required DAO Factory DAOFactory factoryObj = DAOFactory.getDAOFactory(DAOFactory.MYSQL); // Create a DAO UserDAO custDAO = factoryObj.getUserDAO(); // print existing users ArrayList list = custDAO.selectUsers(); Iterator i = list.iterator(); while(i.hasNext()) { User o = (User)i.next(); out.println(o.getName()); } } catch(Exception e) { out.println(e); } finally { out.close(); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload