"显示java.lang.NullPointerException"保存TextField值时 - JavaFx

时间:2013-10-03 16:24:21

标签: java sql nullpointerexception javafx textfield

我正在尝试将TextField值保存到数据库中,但是我收到了“java.lang.NullPointerException”。

我想要发生的是在调用'addClient()'方法时将客户端的名字保存到数据库中。

任何人都可以帮助我开始工作吗?提前谢谢大家。

控制器类:

import java.net.URL;
import java.sql.SQLException;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextField;
import wakiliproject.Forms.AddNew.DB.NewClientDB;

public class NewClientController implements Initializable {

    @FXML
    public TextField firstName;

    // Initializes the controller class.
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
    }

    @FXML
    public void addClient() throws SQLException {
        new NewClientDB().main();
    }
}

数据库类:

import Database.Plain.Skell.DBConnect;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import wakiliproject.Forms.AddNew.NewClientController;

public class NewClientDB extends NewClientController {

    private String firstNames = new NewClientController().firstName.getText();

    public void main() throws SQLException {
        Connection conn = DBConnect.connect();
        Statement stmt = null;
        try {

            //STEP 3: Open a connection
            System.out.println("Connecting to a selected database...");
            System.out.println("Connected database successfully...");

            //STEP 4: Execute a query
            System.out.println("Inserting records into the table...");
            stmt = conn.createStatement();

            String sql = "INSERT INTO KIWI "
                    + "VALUES (102, " + (firstNames) + ")";
            stmt.executeUpdate(sql);
            System.out.println("Inserted records into the table...");

        } catch (SQLException se) {
            //Handle errors for JDBC
            se.printStackTrace();
        } catch (Exception e) {
            //Handle errors for Class.forName
            e.printStackTrace();
        } finally {
            //finally block used to close resources
            try {
                if (stmt != null) {
                    conn.close();
                }
            } catch (SQLException se) {
            }// do nothing
            try {
                if (conn != null) {
                    conn.close();
                }
            } catch (SQLException se) {
                se.printStackTrace();
            }//end finally try
        }//end try
        System.out.println("Goodbye!");
    }//end main
}

修改

我忘了提到这是在JavaFx中,并且从使用JavaFx SceneBuilder创建的FXML文件调用'TextField firstName'。我已经尝试在控制器中初始化它但没有成功。有任何想法吗?我认为这些类型的对象不应该是无限制的,至少是通过JavaFx。

错误:

Caused by: java.lang.NullPointerException
    at wakiliproject.Forms.AddNew.DB.NewClientDB.<init>(NewClientDB.java:12)
    at wakiliproject.Forms.AddNew.NewClientController.addClient(NewClientController.java:41)
    ... 40 more

2 个答案:

答案 0 :(得分:2)

在这一行中,firstName为null

private String firstNames = new NewClientController().firstName.getText();

您必须在构造函数中初始化变量,或者在声明它时

public TextField firstName = new TextField();

答案 1 :(得分:0)

我的猜测是这一行.-

private String firstNames = new NewClientController().firstName.getText();

投掷NullPointerException。您正在创建NewClientController的新实例,但您永远不会初始化firstName实例变量。