0

I use TomCat 8.0 server, h2 database and IntelliJ IDEA 14 for my WebApplication, but when I run app(button event) I receive

java.lang.NullPointerException image. I have successful connection to Database like this(it's already tested):

public class DbConnection {
private static final String DRIVER = "org.h2.Driver";
private static final String URL = "jdbc:h2:tcp://localhost/~/Students";
private static final String USER_NAME = "doncho";
private static final String PASS = "";
private static DbConnection instance;
private static Connection conn;

private DbConnection(){

}

public static DbConnection getInstance(){
    if(instance == null){
        instance = new DbConnection();
    }
    return instance;
}

public Connection getConnect() throws SQLException{
    Connect();
    return conn;
}

private void Connect() throws SQLException {
    if (conn == null) {
        try {
            Class.forName(DRIVER);
        } catch (ClassNotFoundException e) {
            System.out.println("Driver isn't found");
            e.printStackTrace();
        }
        DriverManager.getConnection(URL, USER_NAME, PASS);
    }
}

public void Disconnect() throws SQLException{
    if(conn != null){
        conn.close();
        conn=null;
    }
}

I have create class RegisterDao which must insert data into table with name "Name" and just one column - "name"

public class RegisterDAO {

Connection conn = null;
PreparedStatement state = null;

public StudentBean registerStudent(String userName) throws SQLException{

    StudentBean result = null;
    String sql = "INSERT INTO Name VALUES(?)";

    try {
        conn = DbConnection.getInstance().getConnect();
        state = conn.prepareStatement(sql);
        state.setString(1, userName);
        state.execute();
    } catch (SQLException e) {
        e.printStackTrace();
    }finally {
        if(state != null){
            state.close();
        }
    }
   DbConnection.getInstance().Disconnect();
    return result;
}}

And this is my class (java bean) StudentBean

@javax.ejb.Stateless(name = "StudentEJB")
public class StudentBean {

private String name;


public StudentBean() {
}

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}}

I use thеsе classes to insert data in table "Name" through servlet - RegisterServlet

public class RegisterServlet extends HttpServlet {

protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        Connection conn = DbConnection.getInstance().getConnect();
        System.out.println("It Works");
    } catch (SQLException e) {
        e.printStackTrace();
    }

    String username = request.getParameter("usernameReg");
    StudentBean student;
    try {
        student = new RegisterDAO().registerStudent(username);
        request.getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
    } catch (SQLException e) {
        e.printStackTrace();
    }

    try {
        DbConnection.getInstance().Disconnect();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}}

But I get the upper error. I am confused because I tested source in Eclipse and there it works, but in IntelliJ it doesn't. I'm new in IntelliJ so I hope that some with more experience will give me an idea to resolve the problem.

P.S. System.out.println("It Works"); from servlet works and I guarantee that I call servlet into "index.jsp" correct.

Sorry for this long post and best regards, D.Balamjiev.

mixel
  • 25,177
  • 13
  • 126
  • 165

1 Answers1

0

It works in Eclipse, are you sure about that?

DriverManager.getConnection(URL, USER_NAME, PASS);

You are not assigning it to conn so conn is still null, then conn.prepareStatement throws exception, since conn is null. I think all you need to do is just:

conn = DriverManager.getConnection(URL, USER_NAME, PASS);

Shadov
  • 5,421
  • 2
  • 19
  • 38