rheighy
rheighy

Reputation: 11

Getting Null Pointer in PreparedStatement.close()

im trying to insert values from textfield to mySql using jdbc but my problem is im having a null pointer error when closing my prepared statement. here's my code

 private Connection con;
 private Statement stm;
 private ResultSet rs;
 private PreparedStatement ps ;



public void dbConnect(){
    try{
        Class.forName("com.mysql.jdbc.Driver");
        this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
                + "", "root", "password");

    }
    catch(ClassNotFoundException ex){}
    catch(SQLException ex){}
}

 public boolean insert(Member member)throws SQLException{
    this.dbConnect();
    boolean success = false;
    String query = "INSERT INTO try VALUES(?, ?)";
            try {
        this.ps = this.con.prepareStatement(query);
        this.ps.setString(1, member.getMemberId());
        this.ps.setString(2, member.getFirstName());


       int rows = this.ps.executeUpdate();
        if(rows>0){
            success = true;
        }

    } catch (SQLException ex) {
        ex.printStackTrace();
        throw ex;
    } finally{
        this.ps.close(); // this is my line: 56 (im getting null pointer here)
        this.con.close();
    }

    return success;
}

here's the stacktrace:

   Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at rtu.tomis.dao.MemberDao.insert(MemberDao.java:56)
at rtu.tomis.gui.AddMemberView.btnSaveActionPerformed(AddMemberView.java:993)
at rtu.tomis.gui.AddMemberView.access$800(AddMemberView.java:21)
at rtu.tomis.gui.AddMemberView$9.actionPerformed(AddMemberView.java:476)

thank u for your help

Upvotes: 1

Views: 4546

Answers (5)

Prateek
Prateek

Reputation: 12252

You are not getting Prepared Statement object in your insert method and invoking anything on null give you Null Pointer Exception.

Upvotes: 0

Ulises
Ulises

Reputation: 13429

You need to check for null in the finally clause

finally{
  if(ps != null)
        this.ps.close();
   //
}

but the underlying problem may be when you open your connection, after it fails it will not initialize your prepared statement:

this.ps = this.con.prepareStatement(query);

In other words, before calling prepareStatement make sure con is not null. You should change your method dbConnect and print the exceptions, this will help you take care of the cause of the problem.

 try{
        Class.forName("com.mysql.jdbc.Driver");
        this.con = DriverManager.getConnection("jdbc:mysql://localhost:3306/dbTestDrive"
                + "", "root", "password");

    }
    catch(ClassNotFoundException ex){
     ex.printStackTrace();
    }
    catch(SQLException ex){
     ex.printStackTrace();
    }

Upvotes: 0

Ted Hopp
Ted Hopp

Reputation: 234847

You are getting an exception inside the try block, somewhere before or while this line executes:

this.ps = this.con.prepareStatement(query);

After the catch block rethrows the exception, the finally block executes. Unfortunately, because ps is null, you get another exception, which is then thrown, hiding the original exception.

You need to code more defensively, closing ps and con only if they are not null, and catching any exceptions that the close() calls might themselves raise. This will allow you to propagate any exceptions thrown by the body of the try.

You should also not silently ignore exceptions thrown in the try block of dbConnect(). I suspect that an exception there is the root of your problem.

Upvotes: 3

Abubakkar
Abubakkar

Reputation: 15664

You should write your code like this :

finally{
  if(ps != null)
        try{
            this.ps.close(); 
        }catch(Exception e){
            System.out.println("PreparedStatement close problem");
        }
  if(con != null)
        try{
            this.con.close();
        }catch(Exception e){
            System.out.println("Database Connection close problem");
        }
}

This will avoid null pointer exception if any of the con or ps were not initialized due to some error.

Also you can remove this keyword from all your code because you don't have any local variables of your method with the same name as your class variables , so there won't be any ambiguity

Upvotes: 2

Jj Tuibeo
Jj Tuibeo

Reputation: 773

I do think that there was an exception thrown in your dbConnect() method that's why the jvm reads directly to the finally before reading the instantiation of your prepared statement.

Upvotes: 0

Related Questions