我在setCellValueFactory [closed]上收到空指针异常

I get a null pointer exception on setCellValueFactory [closed]

提问人:ameer anas 提问时间:6/5/2022 最后编辑:Lajos Arpadameer anas 更新时间:6/6/2022 访问量:699

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将帮助其他人回答这个问题。

去年关闭。

我不确定我的代码出了什么问题。我尝试了所有方法,但还没有用。错误如下:

原因:java.lang.NullPointerException:无法调用“javafx.scene.control.TableColumn.setCellValueFactory(javafx.util.Callback)”,因为“this.InvestorIDColumn“为空 在申请时。PagesController.initialize(PagesController.java:93)

代码如下

package application;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;

public class PagesController implements Initializable {
    private static String dbUsername = "root"; // database username
    private static String dbPassword = "Ameer12345!"; // database password
    private static String URL = "127.0.0.1"; // server location
    private static String port = "3306"; // port that mysql uses
    private static String dbName = "univ3"; // database on mysql to connect to
    private static Connection con;

    private Stage stage;
    private Scene scene;
    private Parent root;

    @FXML
    private Button myButton;
    @FXML
    private TextField myTextField1;
    @FXML
    private TextField myTextField2;
    @FXML
    private Label myLabel;

    int username;
    @FXML
    TableView<Investor> myInvestorTableView;
    @FXML
    TableColumn<Investor, String> InvestorNameColumn;
    @FXML
    TableColumn<Investor, Integer> InvestorIDColumn;
    @FXML
    TableColumn<Investor, String> InvestorPNumberColumn;
    @FXML
    TableColumn<Investor, String> InvestorEmailColumn;

    public void switchToMainPage(ActionEvent event) throws IOException, ClassNotFoundException, SQLException {

        try {

            username = Integer.parseInt(myTextField1.getText());
            if (username == 112) {

                Parent root = FXMLLoader.load(getClass().getResource("MainPage.fxml"));
                stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
                scene = new Scene(root);
                stage.setScene(scene);
                stage.show();

            } else
                myLabel.setText("wrong username or password ");
        } catch (Exception e) {
            System.out.println("wrong username or password " + e);
        }

    }

    public void switchToLoginPage(ActionEvent event) throws IOException, ClassNotFoundException, SQLException {

        Parent root = FXMLLoader.load(getClass().getResource("Login.fxml"));
        stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
        scene = new Scene(root);
        stage.setScene(scene);
        stage.show();

    }

    @Override
    public void initialize(java.net.URL arg0, ResourceBundle arg1) {
        InvestorIDColumn.setCellValueFactory(new PropertyValueFactory<Investor, Integer>("investor_id"));
        InvestorPNumberColumn.setCellValueFactory(new PropertyValueFactory<Investor, String>("investor_PNumber"));

        InvestorNameColumn.setCellValueFactory(new PropertyValueFactory<Investor, String>("investor_name"));
        InvestorEmailColumn.setCellValueFactory(new PropertyValueFactory<Investor, String>("investor_Email"));
        try {
            myInvestorTableView.setItems(getInvestor());
        } catch (ClassNotFoundException | SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public ObservableList<Investor> getInvestor() throws ClassNotFoundException, SQLException {

        ObservableList<Investor> Investors = FXCollections.observableArrayList();
        DBConn a = new DBConn(URL, port, dbName, dbUsername, dbPassword);
        con = a.connectDB();
        System.out.println("Connection established");
        String SQLtxt = "select * from investor";
        Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery(SQLtxt);
        while (rs.next()) {
            int ID = Integer.parseInt(rs.getString(1));
            String name = rs.getString(3);
            String Pnumber = rs.getString(2);
            ;
            String Email = rs.getString(4);

            Investors.add(new Investor(name, ID, Email, Pnumber));
        }

        rs.close();
        stmt.close();
        con.close();
        System.out.println("Connection closed");

        return Investors;
    }

}

这是我的班级:

package application;

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;

public class Investor {
    private SimpleStringProperty investor_name;
    private SimpleIntegerProperty investor_id;
    private SimpleStringProperty investor_Email;
    private SimpleStringProperty investor_PNumber;

    public Investor(String investor_name, int investor_id, String investor_Email, String investor_PNumber) {
        this.investor_name = new SimpleStringProperty(investor_name);
        this.investor_id = new SimpleIntegerProperty(investor_id);
        this.investor_Email = new SimpleStringProperty(investor_Email);
        this.investor_PNumber = new SimpleStringProperty(investor_PNumber);
    }

    public SimpleStringProperty getInvestor_name() {
        return investor_name;
    }

    public SimpleIntegerProperty getInvestor_id() {
        return investor_id;
    }

    public SimpleStringProperty getInvestor_Email() {
        return investor_Email;
    }
    
    public SimpleStringProperty getInvestor_PNumber() {
        return investor_PNumber;
    }

}

下面是 FXML 文件:

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Menu?>
<?import javafx.scene.control.MenuBar?>
<?import javafx.scene.control.MenuItem?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.layout.AnchorPane?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.PagesController">
   <children>
      <Button layoutX="531.0" layoutY="361.0" mnemonicParsing="false" onAction="#switchToLoginPage" text="log out" textFill="#bf2626" />
      <MenuBar prefHeight="28.0" prefWidth="576.0" AnchorPane.bottomAnchor="372.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <menus>
          <Menu mnemonicParsing="false" text="File">
            <items>
              <MenuItem mnemonicParsing="false" text="Close" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Edit">
            <items>
              <MenuItem mnemonicParsing="false" text="Delete" />
            </items>
          </Menu>
          <Menu mnemonicParsing="false" text="Help">
            <items>
              <MenuItem mnemonicParsing="false" text="About" />
            </items>
          </Menu>
        </menus>
      </MenuBar>
      <TableView fx:id="myInvestorTableView" layoutY="28.0" prefHeight="200.0" prefWidth="200.0" AnchorPane.bottomAnchor="172.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="28.0">
        <columns>
          <TableColumn fx:id="InvestorIDColumn" prefWidth="155.0" text="InvestorIDColumn" />
          <TableColumn fx:id="InvestorPNumberColumn" prefWidth="145.0" text="InvestorPNumberColumn" />
            <TableColumn fx:id="InvestorNameColumn" prefWidth="132.0" text="InvestorNameColumn" />
            <TableColumn fx:id="InvestorEmailColumn" prefWidth="158.0" text="InvestorEmailColumn" />
        </columns>
      </TableView>
   </children>
</AnchorPane>
java javafx nullpointerexception scenebuilder observablelist

评论

2赞 trashgod 6/5/2022
注意命名约定:为什么我应该避免在 JavaFX 中使用 PropertyValueFactory,我应该使用什么来代替?
1赞 James_D 6/5/2022
同时发布完整的堆栈跟踪
0赞 VGR 6/5/2022
Investor 类的方法名称不正确。看看 Node 的方法。返回 Property 的方法的命名约定是什么?名称以 开头的方法的返回类型是什么?get

答:

1赞 Lajos Arpad 6/6/2022 #1

这是抛出错误的行:

InvestorIDColumn.setCellValueFactory(new PropertyValueFactory<Investor, Integer>("investor_id"));

此数据成员声明为

@FXML
TableColumn<Investor, Integer> InvestorIDColumn;

但似乎在调用您的方法时它(还没有)值。要解决此问题,您需要初始化此数据成员(如果状态无效),或者您可以检查它是否是,并且仅在它不是时调用。initializenullnullsetCellValueFactorynull

对于XML中的某些记录,投资者ID列可能是空的。