繁体   English   中英

我该如何解决这个 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 正在返回 null 因为 connectDB 很好,这里是:

    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 连接。

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;
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM