Hello, please help with the malfunction of the program, I get a NullPointerException в строке preparedStatement = connection.prepareStatement(sql); I suspect a problem in the connection, in the getConn method, but not sure.

 public class ProfileDAOImpl implements ProfileDAO { Connection connection = null; PreparedStatement preparedStatement = null; public Connection getConn() throws Exception{ Properties properties = new Properties(); try { properties.load(new FileInputStream("info.properties")); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String dburl = properties.getProperty("dburl"); String driver = properties.getProperty("driver"); Class.forName(driver); if (connection == null) { connection = DriverManager.getConnection(user, password, dburl); } } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } private PreparedStatement getPreparedStatement(String sql) throws DAOException{ if(preparedStatement == null) { try { preparedStatement = connection.prepareStatement(sql); } catch (SQLException e) { e.printStackTrace(); } } return preparedStatement; } public void updateProfile(Profile profile) throws DAOException { String sql = "UPDATE profiles SET id=?, user_name=?," + " nick_name=?, user_mail=?, password=?;"; try { getPreparedStatement(sql); preparedStatement.setInt(1, profile.getId()); preparedStatement.setString(2, profile.getUserName()); preparedStatement.setString(3, profile.getNickName()); preparedStatement.setString(4, profile.getUserMail()); preparedStatement.setString(5, profile.getPassword()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void closeConnection() { try { if (preparedStatement != null) { preparedStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } } public interface ProfileDAO { public void updateProfile(Profile profile) throws DAOException; public void closeConnection(); } public class Main { public static void main(String[] args) throws DAOException { ProfileDAO profileDAO = new ProfileDAOImpl(); Profile profile = new Profile(2,"Test","Nick","test@test","123q"); profileDAO.updateProfile(profile); profileDAO.closeConnection(); } } 
  • But you have debag in the IDE? Why not use it? - Alexey Shimansky
  • Of course, you will have Connection null , because you never call the getConn() method in which this object is initialized. - enzo
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

As already noted in the comments, you do not call the getConn() method - hence the NullPointerException .

If you have a connection and preparedStatement always reused, then it is better to transfer their initialization to the constructor. Then ProfileDAOImpl will turn out like this:

 public class ProfileDAOImpl implements ProfileDAO { final Connection connection; final PreparedStatement updateProfileStatement; public ProfileDAOImpl() throws Exception { Properties properties = new Properties(); properties.load(new FileInputStream("info.properties")); String user = properties.getProperty("user"); String password = properties.getProperty("password"); String dburl = properties.getProperty("dburl"); String driver = properties.getProperty("driver"); Class.forName(driver); this.connection = DriverManager.getConnection(user, password, dburl); this.updateProfileStatement = connection.prepareStatement("UPDATE profiles SET id=?, user_name=?," + " nick_name=?, user_mail=?, password=?"); } public void updateProfile(Profile profile) throws DAOException { try { updateProfileStatement.setInt(1, profile.getId()); updateProfileStatement.setString(2, profile.getUserName()); updateProfileStatement.setString(3, profile.getNickName()); updateProfileStatement.setString(4, profile.getUserMail()); updateProfileStatement.setString(5, profile.getPassword()); updateProfileStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void closeConnection() { try { if (updateProfileStatement != null) { updateProfileStatement.close(); } } catch (SQLException e) { e.printStackTrace(); } try { if (connection != null) { connection.close(); } } catch (Exception e) { e.printStackTrace(); } } } 
  • getConn's behavior is not set to throw DAOE, how to implement it correctly? - PolkovnikJ
  • one
    If you want to throw out some kind of your own exception, then you just make a wrapper, like this: try {// method body} catch (Exception e) {throw new DAOException (e); } - Alex K