JavaFX TableView NullPointerException

JavaFX TableView NullPointerException,java,javafx,nullpointerexception,treeview,tableview,Java,Javafx,Nullpointerexception,Treeview,Tableview,我有一个在JavaFXTreeView中显示数据库结果的应用程序。我做的和书中提到的一模一样。 但是当我运行这个程序时,我得到一个如下的错误错误指向控制器中的tblView.setItems(数据)方法 我该如何解决这个问题 java.lang.NullPointerException at demofx.FXMLDocumentController.buildData(FXMLDocumentController.java:105) at demofx.FXMLDocument

我有一个在JavaFXTreeView中显示数据库结果的应用程序。我做的和书中提到的一模一样。 但是当我运行这个程序时,我得到一个如下的错误
错误指向控制器中的
tblView.setItems(数据)
方法

我该如何解决这个问题

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)


这是我的.fxml文档

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

问题解决了,在控制器中我声明了一个
TableView tblView
,但在
.fxml
中它是
tblvehile
。两个名称应该相同,因此我将控制器更改为
tblvehichile
。现在它开始工作了

一种方法是对
表视图使用正确的
fx:id
。。。顺便说一句:在你的代码中,树视图在哪里?@fabian my bad。。我不应该是tableview
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();
        }
}

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

}