Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6732407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:38:48+00:00 2026-05-26T10:38:48+00:00

I tried out DAO Pattern after reading http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html but I get NullPointerException when I

  • 0

I tried out DAO Pattern after reading
http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
but I get NullPointerException when I run the servlet

DAOFactory

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;
}
}
}

MySQLDAOFactory

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();
}
}

MySQLUserDAO

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<User> selectUsers() {
    ResultSet rs=null;
    ArrayList customerList=null;
    try{
        rs =s.executeQuery("select * from users");
        customerList = new ArrayList<User>();
        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;
}
}

UserDAO

package dao;
import java.util.ArrayList;
import model.User;

public interface UserDAO {
public int insertUser(String fname, String lname);
public ArrayList<User> selectUsers();
} 

model.User.java

This class has the following fields and the corresponding accessor methods.

String name;
String pass;
short type;
String email;

My Servlet file

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:38:49+00:00Added an answer on May 26, 2026 at 10:38 am

    Stack trace points to MySQLUserDAO class and line s = conn.createStatement()

    So, the conn is null.

    Let’s look how you’re creating the conn:

    public static Connection createConnection() {
        Connection conn=null;
        try{
            Class.forName(DRIVER);
            conn =  DriverManager.getConnection(DBURL,"root","toor");
        }catch(Exception e){}
        return conn;
    }
    

    So, you’re ignoring any exception thrown and returning a null connection instead of throwing an exception so that the code immediately stops.

    Your technical problem is caused by the driver apparently not being in the classpath and your real design problem is you should never ignore exceptions unless you’re 100% sure that it’s safe to continue the code flow.

    It’s by the way unnecessary to load the driver everytime. Just load it once on class’ initialization.

    Rewrite that piece as follows:

    static {
        try{
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            throw new ExceptionInInitializerError("Driver " + DRIVER + " missing in classpath", e);
        }
    }
    
    public static Connection createConnection() throws SQLException {
        return DriverManager.getConnection(DBURL, "root", "toor");
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have more than one OpenID as I have tried out numerous. As people
I am very interested in streaming data for web-applications. I have tried out some
I've tried figuring out this problem for the last 2 days with no luck.
Well I tried to figure out is this possible in any way. Here is
Long ago I tried to sort out my system between local, web server and
For a long time i have tried to work out the best way to
I've tried my best and cannot figure out what happened here. It worked fine
I tried searching around, but I couldn't find anything that would help me out.
I was trying to check out a project from SVN using Eclipse. I tried
Silly question, but I'm unable to figure out.. I tried the following in Ruby:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.