-1

how to fix error java.lang.NullPointerException: Cannot invoke "java.sql.Connection.createStatement()" because "con" is null

anh14
  • 1
  • 1
  • 3
  • package dow; import java.sql.*; /** * * @author ANH */ public class ConnectionProvider { public static Connection getCon(){ try{ Class.forName("com.mysql.jdbc.Driver"); Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","anh14"); Statement st = con.createStatement(); return con; } catch(Exception e){ return null; } } } – anh14 Mar 26 '22 at 16:10
  • package dow; import javax.swing.JOptionPane; import java.sql.*; /** * * @author ANH */ public class DbOperations { public static void setDataOrDelete(String Query, String msg){ try{ Connection con = ConnectionProvider.getCon(); Statement st = con.createStatement(); st.executeUpdate(Query); if(!msg.equals("")) JOptionPane.showMessageDialog(null, msg); } catch(Exception e){ JOptionPane.showMessageDialog(null, e, "Message", JOptionPane.ERROR_MESSAGE); } } } – anh14 Mar 26 '22 at 16:10
  • package dow; import javax.swing.JOptionPane; public class tables { public static void main(String[] args){ try{ String userTable= "create table user(id int AUTO_INCREMENT primary key,name varchar(200),email varchar(200),mobileNumber varchar(10),address varchar(200),password varchar(200),securityQuestion varchar(200),answer varchar(200),status varchar(20),UNIQUE(email))"; DbOperations.setDataOrDelete(userTable, "User Table Created Successfully"); } catch(Exception e){ JOptionPane.showMessageDialog(null, e); } } } – anh14 Mar 26 '22 at 16:11

1 Answers1

0

Fix your exception handling. The error you got cannot occur unless you wrote broken handling. This is extremely common in answers and tutorials but is also completely wrong and not something you should EVER write:

try {
  con = DriverManager.blablabla;
} catch (Exception e) {
  e.printStackTrace();
}

The reason that is wrong is that you clearly have no idea what it means when that exception occurs (you just print it, that's all you do), which isn't a complaint: MOST exceptions fall in that category - that you either don't really know what happened or have no chance of recovery even if you do. The problem isn't in doing nothing useful in the catch block - the problem is that your code continues. When an unrecoverable situation has occurred or when you haven't investigated what it means. That's riduclous. Don't do that.

The proper way to deal with exceptions when you don't want to bother / there is no point as recovery is impossible, is, in order of preference:

  • Just throw it onwards. public static void main(String[] args) throws Exception is what your main method should look like. You don't want to catch unrecoverable exceptions in the first place!

  • If that's somehow impossible (API design dictates that'd be bad style, or due to limitations as you're overriding methods or in lambdas, you can't), then the 'I do not know what to do / cannot recover' default catch block should be:

try {
  // stuff
} catch (Exception e) {
  throw new RuntimeException("unhandled", e);
}

And NOT e.printStackTrace(). Once you've fixed that, this error will no longer occur. At that point, read the error you see - perhaps you have the wrong password, the mysql server is down, who knows. The error will tell you, but this question indicates that error has gotten lost someplace, or you're getting a billion errors and don't know which of the many many lines of error are relevant (the one you pasted isn't the problem).

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72