提问人:zee ilyas 提问时间:9/23/2020 最后编辑:zee ilyas 更新时间:9/23/2020 访问量:5928
如何解决此java.lang.NullPointerException?
how do i resolve this java.lang.NullPointerException?
问:
我正在尝试运行在HSQL和MySQL数据库上运行的应用程序,当我运行它时,出现此错误:
java.lang.NullPointerException: Cannot invoke "java.sql.Connection.prepareStatement(String)" because "this.cnn" is null
at DataBase.DBcontrol.creer_piece(DBcontrol.java:122)
at pdr.FrontController.initialize(FrontController.java:160)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2573)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2466)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3237)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3194)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3163)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3136)
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3113)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3106)
at pdr.Main.start(Main.java:13)
at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:832)
它在前几行中引用的代码是这样的:
public void creer_piece() throws ClassNotFoundException {
HConnexion dbc = new HConnexion(); cnn=dbc.connectDb();
PreparedStatement pss;
ResultSet s;
try {
req = "CREATE TABLE IF NOT EXISTS moussa.piece "
+ " (id VARCHAR(30), "
+ " atelier VARCHAR(30), "
+ " piece VARCHAR(30), "
+ " type VARCHAR(30) NULL, "
+ " pa VARCHAR(2) NULL, "
+ " ref1 VARCHAR(10) NULL , "
+ " ref2 VARCHAR(10) NULL , "
+ " quant INTEGER,"
+ " emp VARCHAR(30),"
+ " pos VARCHAR(30),"
+ " UNIQUE (id))";
pss = cnn.prepareStatement(req);
int e = pss.executeUpdate();
if (e > 0) {
JOptionPane.showMessageDialog(null, " erraurgg");
}
} catch (SQLException ex) {
JOptionPane.showMessageDialog(null, ex);
}
还有这个:
public void initialize(URL url, ResourceBundle rb) {
try {
run_wamp();
timer.schedule(new TimerTask() {
@Override
public void run() {
}
},10000);
dbc.creer_piece();dbc.creer_refs();dbc.creer_carte();dbc.creer_module();dbc.creer_eqip();
dbc.creer_c8();dbc.creer_secteur();dbc.creer_unite();dbc.creer_eqip();
dbc.creer_atelier();
dbc.get_natelier("SELECT * FROM moussa.atelier",combo_piece1);
} catch (Exception ex) {
Logger.getLogger(FrontController.class.getName()).log(Level.SEVERE, null, ex);
}
我认为这与我的数据库有关,要么应用程序没有创建表,要么我应该手动创建表并连接它们,虽然我只是在这里吐口水,但需要一些帮助。
编辑:所以在看了评论之后,你们告诉我,CNN因为connectDB而返回null,这里是:
public class HConnexion {
private static String msg ;
Connection conn = null;Statement stm =null;
//=====================================================================================
public Connection connectDb() throws ClassNotFoundException {
String s = System.getProperty("user.home");
String JDBC_URL = "jdbc:hsqldb:Pdr_db;";
run_wamp();
String path= "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull";
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(JDBC_URL, "root", ""); //hsql database
conn = DriverManager.getConnection(path, "root", ""); //mysql database
stm=conn.createStatement();
stm.executeUpdate("create database IF NOT EXISTS moussa ");
return conn;
} catch (SQLException e) {
JOptionPane.showMessageDialog(null, e);
return null;
}
}
这个方法(如果我错了,请纠正我)应该将我连接到我的数据库(或者创建一个新数据库,以防万一),所以如果 CNN 返回 null,这是否意味着它没有创建其他方法可以通过 CNN 连接的数据库。
答:
0赞
fredt
9/23/2020
#1
null 指针异常是由返回 null 的不正确的连接代码引起的。
// code for HSQLDB
public class HConnexion {
private static String msg = "Cannot connect";
Connection conn = null;Statement stm =null;
public Connection connectDb() throws ClassNotFoundException, SQLException {
String s = System.getProperty("user.home");
String JDBC_URL = "jdbc:hsqldb:Pdr_db;"; // this is a HSQLDB URL
run_wamp();
// String path= "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull"; // we leave this out as you can connect only to one database, not two
try {
Class.forName("org.hsqldb.jdbc.JDBCDriver");
// Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(JDBC_URL, "root", ""); //hsql database - used
// conn = DriverManager.getConnection(path, "root", ""); //mysql database - unused
stm=conn.createStatement();
// stm.executeUpdate("create database IF NOT EXISTS moussa "); // only for mysql
return conn;
} catch (SQLException e) {
JOptionPane.showMessageDialog(msg, e);
throw e;
}
}
如果要连接到 MySQL,请使用以下代码
// code for MySQL
public class HConnexion {
private static String msg = "Cannot connect";
Connection conn = null;Statement stm =null;
public Connection connectDb() throws ClassNotFoundException, SQLException {
String s = System.getProperty("user.home");
// String JDBC_URL = "jdbc:hsqldb:Pdr_db;"; // this is a HSQLDB URL
run_wamp();
String path= "jdbc:mysql://localhost:3306/mysql?zeroDateTimeBehavior=convertToNull"; // we use this for MySQL
try {
// Class.forName("org.hsqldb.jdbc.JDBCDriver");
Class.forName("com.mysql.jdbc.Driver");
// conn = DriverManager.getConnection(JDBC_URL, "root", ""); //hsql database - unused
conn = DriverManager.getConnection(path, "root", ""); //mysql database - used
stm=conn.createStatement();
stm.executeUpdate("create database IF NOT EXISTS moussa "); // only for mysql
return conn;
} catch (SQLException e) {
JOptionPane.showMessageDialog(msg, e);
throw e;
}
}
评论
cnn=dbc.connectDb()
导致为 null。因此,问题出在 .我最好的猜测是您的连接凭据错误或不存在。cnn
dbc.connectDb()
conn