当前位置: 首页 > 知识库问答 >
问题:

JavaFX TableView NullPointerException

萧修永
2023-03-14

我有在JavaFX TreeView中显示数据库结果应用程序。我完全按照这个答案所说的去做。但当我运行该程序时,我得到一个如下所示的错误。
错误指向Controller中的tblview.setitems(data)方法。

我该怎么解决这个..?

java.lang.NullPointerException
    at demofx.FXMLDocumentController.buildData(FXMLDocumentController.java:105)
    at demofx.FXMLDocumentController.initialize(FXMLDocumentController.java:85)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at demofx.DemoFX.start(DemoFX.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
<AnchorPane id="AnchorPane" prefHeight="537.0" prefWidth="736.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="demofx.FXMLDocumentController">
   <children>
      <Label fx:id="lblAuto_id" layoutX="53.0" layoutY="31.0" text="Label" textFill="#da1b1b">
         <font>
            <Font size="19.0" />
         </font>
      </Label>
      <Label fx:id="lblVtype" layoutX="229.0" layoutY="89.0" text="Selected type" />
      <TableView fx:id="tblVehicle" layoutX="38.0" layoutY="142.0" prefHeight="200.0" prefWidth="660.0">
        <columns>
          <TableColumn fx:id="c_reg" prefWidth="71.0" text="Reg no" />
          <TableColumn fx:id="c_make" prefWidth="79.0" text="Make" />
            <TableColumn fx:id="c_model" prefWidth="75.0" text="Model" />
            <TableColumn fx:id="c_year" prefWidth="75.0" text="year" />
            <TableColumn fx:id="c_color" prefWidth="75.0" text="color" />
            <TableColumn fx:id="c_odo" prefWidth="75.0" text="odo" />
            <TableColumn fx:id="c_status" prefWidth="75.0" text="status" />
            <TableColumn fx:id="c_fuel" prefWidth="75.0" text="fuel" />
            <TableColumn fx:id="c_type" prefWidth="75.0" text="type" />
        </columns>
      </TableView>
      <ComboBox fx:id="cmbVehicleType" layoutX="30.0" layoutY="85.0" onAction="#handleButtonAction" prefWidth="150.0" />
   </children>
</AnchorPane>
public class FXMLDocumentController implements Initializable {

    @FXML
    private Label lblAuto_id;
    @FXML
    private ComboBox cmbVehicleType;
    @FXML
    private Label lblVtype;

    @FXML private TableView<Veh> tblView;

    @FXML private TableColumn <Veh,String>  c_reg;
    @FXML private TableColumn <Veh,String>  c_make;
    @FXML private TableColumn <Veh,String>  c_model;
    @FXML private TableColumn <Veh,String>  c_year;
    @FXML private TableColumn <Veh,String>  c_color;
    @FXML private TableColumn <Veh,String>  c_odo;
    @FXML private TableColumn <Veh,String>  c_status;
    @FXML private TableColumn <Veh,String>  c_fuel;
    @FXML private TableColumn <Veh,String>  c_type;

    private ObservableList<Veh> data;



    DBConnection db = new DBConnection();

    @FXML
    private void handleButtonAction(ActionEvent event) {

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) {

        c_reg.setCellValueFactory(new PropertyValueFactory<Veh, String>("regno"));
        c_make.setCellValueFactory(new PropertyValueFactory<Veh, String>("make"));
        c_model.setCellValueFactory(new PropertyValueFactory<Veh, String>("model"));
        c_year.setCellValueFactory(new PropertyValueFactory<Veh, String>("type"));
        c_color.setCellValueFactory(new PropertyValueFactory<Veh, String>("status"));
        c_odo.setCellValueFactory(new PropertyValueFactory<Veh, String>("color"));

       buildData();
    } 

    public void buildData() {

        try {
            Connection con = db.get_connection();
            data = FXCollections.observableArrayList();
            PreparedStatement stat = con.prepareStatement("SELECT * FROM Vehicle_tbl WHERE Status= 'IN' and V_type='CAR'");
            ResultSet rs = stat.executeQuery();
             while (rs.next()) {
                 Veh ve = new Veh();
                ve.regno.set(rs.getString(1));
                ve.make.set(rs.getString(2));
                ve.model.set(rs.getString(3));
                ve.color.set(rs.getString(4));
                ve.status.set(rs.getString(5));
                ve.type.set(rs.getString(6));
                data.add(ve);
            }
            tblView.setItems(data);

        } catch (Exception ex) {
            ex.printStackTrace();
        }
}

}

这是POJO类

public class Veh {

   public SimpleStringProperty regno = new SimpleStringProperty(); 
   public SimpleStringProperty make = new SimpleStringProperty();
   public SimpleStringProperty model = new SimpleStringProperty();
   public SimpleStringProperty type = new SimpleStringProperty();
   public SimpleStringProperty status = new SimpleStringProperty();
   public SimpleStringProperty color = new SimpleStringProperty();

   public String getRegno(){
       return regno.get();
   }

   public String getMake(){
       return make.get();
   }

   public String getModel(){
       return model.get();
   }

   public String getType(){
       return type.get();
   }

   public String getStatus(){
       return status.get();
   }

   public String getColor(){
       return color.get();
   }

}

共有1个答案

柴磊
2023-03-14

问题解决了,在controller中,我声明了一个TableView TBLView,但在.fxml中,它是TBLVehicle。这两个名称应该相同,所以我将controller更改为tblvehicle。现在起作用了

 类似资料:

相关问答

相关文章

相关阅读