JavaFX场景未启动。给出“javafx.fxml.FXMLLoader.constructLoadException”

时间:2017-11-13 05:54:57

标签: javafx scenebuilder

在我的应用程序中,我有两个视图作为RootLayout.fxml和ProductView.fxml,在我的main方法中,我初始化RootLayout并在其中查看ProductView。

public class Main extends Application {
private Stage primaryStage;

private BorderPane rootLayout;

@Override
public void start(Stage primaryStage) {
    this.primaryStage = primaryStage;

    this.primaryStage.setTitle("Stock-Keeper");

    initRootLayout();

    showProductView();
}

    public void initRootLayout() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/RootLayout.fxml"));
        rootLayout = (BorderPane) loader.load();

        Scene scene = new Scene(rootLayout); //We are sending rootLayout to the Scene.
        primaryStage.setScene(scene); //Set the scene in primary stage.

        primaryStage.show(); //Display the primary stage
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public void showProductView() {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(Main.class.getResource("view/ProductView.fxml"));
        AnchorPane productOperationsView = (AnchorPane) loader.load();

        rootLayout.setCenter(productOperationsView);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public static void main(String[] args) {

    Application.launch(args);
}

在RootLayout

的中心查看的ProductView.fxml
<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ChoiceBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Separator?>
<?import javafx.scene.control.TableColumn?>
<?import javafx.scene.control.TableView?>
<?import javafx.scene.control.TextArea?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.VBox?>
<?import javafx.scene.text.Font?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="483.0" prefWidth="688.0" xmlns="http://javafx.com/javafx/8.0.141" fx:controller="sample.controller.ProductController" xmlns:fx="http://javafx.com/fxml/1">
<children>
    <TextField fx:id="productId" layoutX="215.0" layoutY="32.0" prefHeight="26.0" prefWidth="95.0" />
    <Label layoutX="222.0" layoutY="13.0" text="Product ID" />
    <Button fx:id="searchProductBtn" layoutX="217.0" layoutY="68.0" mnemonicParsing="false" onAction="#searchProducts" prefHeight="26.0" prefWidth="69.0" text="Search" />
    <Button fx:id="deleteProductBtn" layoutX="416.0" layoutY="71.0" mnemonicParsing="false" onAction="#deleteProduct" prefHeight="26.0" prefWidth="101.0" text="Delete" />
    <Button fx:id="updateEmpBtn" layoutX="316.0" layoutY="70.0" mnemonicParsing="false" onAction="#updateProduct" prefHeight="24.0" prefWidth="79.0" text="Update" />
    <Button fx:id="addEmpBtn" layoutX="102.0" layoutY="298.0" mnemonicParsing="false" onAction="#insertProduct" text="Add Product" />
    <TextArea fx:id="resultArea" layoutX="8.0" layoutY="372.0" prefHeight="5.0" prefWidth="167.0" wrapText="true" />
    <Label layoutX="13.0" layoutY="350.0" text="Result Console">
        <font>
            <Font name="System Bold" size="12.0" />
        </font>
    </Label>
    <TextField fx:id="newUnitPrice" layoutX="381.0" layoutY="39.0" prefHeight="26.0" prefWidth="159.0" />
    <Label layoutX="426.0" layoutY="16.0" text="New Unit Price" />
    <VBox layoutX="97.0" layoutY="24.0" spacing="4.0">
        <children>
            <TextField fx:id="title" prefHeight="26.0" prefWidth="99.0" />
        </children>
       </VBox>
       <VBox layoutX="9.0" layoutY="28.0" prefHeight="166.0" prefWidth="80.0" spacing="12.0">
          <children>
            <Label text="Title" />
            <Label prefHeight="27.0" prefWidth="32.0" text="Type" />
            <Label prefHeight="25.0" prefWidth="82.0" text="Unit Price" />
        <Label prefHeight="25.0" prefWidth="82.0" text="Quantity" />
        <Label prefHeight="25.0" prefWidth="82.0" text="Description" />
        </children>
    </VBox>
    <Separator layoutY="14.0" prefHeight="0.0" prefWidth="662.0" />
    <Separator layoutX="205.0" layoutY="14.0" orientation="VERTICAL" prefHeight="427.0" prefWidth="8.0" />
    <TableView fx:id="employeeTable" editable="true" layoutX="215.0" layoutY="102.0" prefHeight="288.0" prefWidth="499.0" tableMenuButtonVisible="true">
        <columns>
            <TableColumn fx:id="empIdColumn" prefWidth="57.0" text="Id" />
            <TableColumn fx:id="empNameColumn" prefWidth="100.0" text="Title" />
            <TableColumn fx:id="empLastNameColumn" prefWidth="143.0" text="Type" />
            <TableColumn fx:id="empEmailColumn" prefWidth="89.0" text="Unit Price" />
            <TableColumn fx:id="empPhoneNumberColumn" prefWidth="94.0" text="Quantity" />
        </columns>
    </TableView>
    <Button fx:id="searchEmpsBtn" layoutX="528.0" layoutY="70.0" mnemonicParsing="false" onAction="#searchProducts" prefHeight="26.0" prefWidth="185.0" text="VIew All Products" />
  <Label layoutX="596.0" layoutY="17.0" text="New Quantity" />
  <TextField fx:id="newQuantity" layoutX="589.0" layoutY="38.0" prefHeight="25.0" prefWidth="120.0" />
      <TextField fx:id="unitPrice" layoutX="98.0" layoutY="92.0" prefHeight="26.0" prefWidth="96.0" />
  <ChoiceBox fx:id="type" layoutX="97.0" layoutY="58.0" prefHeight="26.0" prefWidth="96.0" />
  <TextField fx:id="quantity" layoutX="98.0" layoutY="132.0" prefHeight="26.0" prefWidth="95.0" />
  <TextArea fx:id="description" layoutX="17.0" layoutY="203.0" prefHeight="68.0" prefWidth="185.0" />
 </children>
</AnchorPane>

ProductController.java,它是ProductView

的后端处理
package sample.controller;

import javafx.beans.property.IntegerProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import sample.model.Product;
import sample.model.ProductDAO;
import java.sql.SQLException;

  public class ProductController {

  @FXML
  private TextField productId;
  @FXML
  private TextArea resultArea;
  @FXML
  private TextArea description;
  @FXML
  private TextField newUnitPrice;
  @FXML
  private TextField title;
  @FXML
  private TextField newQuantity;
  @FXML
  private TextField unitPrice;
  @FXML
  private ChoiceBox<String> type;
  @FXML
  private TextField quantity;
  @FXML
  private TableView productTable;
  @FXML
  private TableColumn<Product, Integer> productIdColumn;
  @FXML
  private TableColumn<Product, String> productTitleColumn;
  @FXML
  private TableColumn<Product, String> productTypeColumn;
  @FXML
  private TableColumn<Product, Integer> productUnitPriceColumn;
  @FXML
  private TableColumn<Product, Integer> productQuantityColumn;

int unitPriceInt = Integer.parseInt(unitPrice.getText());
int quantityInt = Integer.parseInt(quantity.getText());
int newUnitPriceInt = Integer.parseInt(newUnitPrice.getText());
int newQuantityInt = Integer.parseInt(newQuantity.getText());
int productIdInt = Integer.parseInt(productId.getText());



   //Search a product
   @FXML
   private void searchProduct(ActionEvent actionEvent) throws ClassNotFoundException, SQLException {
    try {
        //Get Product information
        Product product = ProductDAO.searchProduct(productId.getText());
        //Populate Product on TableView and Display on TextArea
        populateAndShowProduct(product);
    } catch (SQLException e) {
        e.printStackTrace();
        resultArea.setText("Error occurred while getting product information from DB.\n" + e);
        throw e;
    }
}

   //Search all products
   @FXML
   private void searchProducts(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
    try {
        //Get all Employees information
        ObservableList<Product> empData = ProductDAO.searchProducts();
        //Populate Products on TableView
        populateProducts(empData);
    } catch (SQLException e) {
        System.out.println("Error occurred while getting product information from DB.\n" + e);
        throw e;
    }
}

    //Initializing the controller class.
    //This method is automatically called after the fxml file has been loaded.
    @FXML
    private void initialize() {
    productIdColumn.setCellValueFactory(cellData -> cellData.getValue().productIdProperty().asObject());
    productTitleColumn.setCellValueFactory(cellData -> cellData.getValue().titleProperty());
    productTypeColumn.setCellValueFactory(cellData -> cellData.getValue().typeProperty());
    productUnitPriceColumn.setCellValueFactory(cellData -> cellData.getValue().unitPriceProperty().asObject());
    productQuantityColumn.setCellValueFactory(cellData -> cellData.getValue().quantityProperty().asObject());

}

    //Populate Product
    @FXML
    private void populateProduct(Product product) throws   ClassNotFoundException {
    //Declare and ObservableList for table view
    ObservableList<Product> productData = FXCollections.observableArrayList();
    //Add employee to the ObservableList
    productData.add(product);
    //Set items to the employeeTable
    productTable.setItems(productData);
}

    //Set Employee information to Text Area
    @FXML
private void setProductInfoToTextArea(Product product) {
    resultArea.setText("First Name: " + product.getTitle());
}

    //Populate Employee for TableView and Display Employee on TextArea
    @FXML
    private void populateAndShowProduct(Product product) throws ClassNotFoundException {
    if (product != null) {
        populateProduct(product);
        setProductInfoToTextArea(product);
    } else {
        resultArea.setText("This product does not exist!\n");
    }
}

    //Populate Employees for TableView
    @FXML
    private void populateProducts(ObservableList<Product> productData) throws ClassNotFoundException {
    //Set items to the productsTable
    productTable.setItems(productData);
}

    //Update employee's email with the email which is written on newUnitPrice field
    @FXML
    private void updateProduct(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
    try {
        ProductDAO.updateProduct(productIdInt, newUnitPriceInt, newQuantityInt);
        resultArea.setText("Product has been updated for, product id: " + productId.getText() + "\n");
    } catch (SQLException e) {
        resultArea.setText("Problem occurred while updating product: " + e);
    }
}

    //Insert a product to the DB
    @FXML
    private void insertProduct(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
    try {
        ProductDAO.insertProduct(title.getText(), type.getValue().toString(),description.getText(),unitPriceInt,quantityInt);
        resultArea.setText("Product inserted! \n");
    } catch (SQLException e) {
        resultArea.setText("Problem occurred while inserting product item" + e);
        throw e;
    }
}

    //Delete a product with a given product Id from DB
    @FXML
    private void deleteProduct(ActionEvent actionEvent) throws SQLException, ClassNotFoundException {
    try {
        ProductDAO.deleteProductWithId(productId.getText());
        resultArea.setText("Product deleted! Product id: " + productId.getText() + "\n");
    } catch (SQLException e) {
        resultArea.setText("Problem occurred while deleting product " + e);
        throw e;
       }
    }
 }

仅初始化RootLayout但不查看ProductView。

它给出了这个错误。

    javafx.fxml.LoadException: 
/home/jaliya/github/Test/out/production/Test/sample/view/ProductView.fxml:15

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at sample.Main.showProductView(Main.java:70)
    at sample.Main.start(Main.java:38)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$50(GtkApplication.java:139)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException
    at sample.controller.ProductController.<init>(ProductController.java:51)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
    at java.lang.Class.newInstance(Class.java:442)
    at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
    at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:927)
    at javafx.fxml.FXMLLoader$InstanceDeclarationElement.processAttribute(FXMLLoader.java:971)
    at javafx.fxml.FXMLLoader$Element.processStartElement(FXMLLoader.java:220)
    at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:744)
    at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
    ... 13 more

0 个答案:

没有答案