初始化时FXML加载异常

时间:2019-03-17 11:50:52

标签: java javafx java-8

由于某种原因,每当我添加一个将切换场景的按钮时,我都会不断出错。我一生无法理解错误在做什么。当我尝试初始化组合框时也会发生这种情况。我已经坚持了很长时间,开始怀疑我是否在其他地方错过了错误

enter image description here 错误。

FirstPage.fxml代码

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>

<AnchorPane prefHeight="518.0" prefWidth="708.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FilmsController">
   <children>
      <Button fx:id="mainpagebutton" layoutX="86.0" layoutY="154.0" mnemonicParsing="false" onAction="#startpage" text="Start Button" />
      <Button layoutX="41.0" layoutY="-114.0" mnemonicParsing="false" text="Button" />
      <HBox layoutX="-166.0" layoutY="-137.0" prefHeight="123.0" prefWidth="474.0" />
   </children>
</AnchorPane>

主页

    package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("FirstPage.fxml"));
        primaryStage.setTitle("");
        primaryStage.setScene(new Scene(root, 708, 518));
        primaryStage.show();
    }


    public static void main(String[] args) {

        launch(args);
    }
}

FilmsController页面

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.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.stage.Modality;
import javafx.stage.Stage;
import javafx.stage.Window;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.ResourceBundle;


public class FilmsController implements Initializable{
    private ArrayList<String> filmtitles = new ArrayList<>();
    private ArrayList<Films> films = new ArrayList<>();

    @FXML
    public Button receiptbut;
    @FXML
    public ComboBox<String> filmcombobox;
    @FXML
    public Button mainpagebutton;
    @FXML
    private Button leavepagebutton;

    ObservableList<String> olfilmtitles = FXCollections.observableArrayList(
            "Matrix", "Avatar", "Incredibles", "The Grinch", "Harry Potter", "Ice Age", "Home Alone",
            "E.T.","Shrek", "Wonder"
    );
    public FilmsController() {
        Films matrix = new Films("Matrix", 5, 15, 120, "2:30");
        films.add(matrix);

    }
    public void drinksPopUpStage(ActionEvent actionEvent) throws IOException{
        Scene drinksscene = new Scene(FXMLLoader.load(getClass().getResource("../FXMLfolder/Drinks.fxml")));
        Stage Drinks = new Stage();
        Drinks.setScene(drinksscene);
        Drinks.initModality(Modality.APPLICATION_MODAL);
        Drinks.show();
    }


    public void snacksPopUpStage(ActionEvent actionEvent) throws IOException{
        Scene snacksscene = new Scene(FXMLLoader.load(getClass().getResource("../FXMLfolder/Snacks.fxml")));
        Stage Snacks = new Stage();
        Snacks.setScene(snacksscene);
        Snacks.initModality(Modality.APPLICATION_MODAL);
        Snacks.show();
    }

    public void changetitle(ActionEvent actionEvent) {
    }


   /* public ObservableList<String> getFilmtitles() {
        for (Films film : films) {
            filmtitles.add(film.getName());
        }
        ObservableList<String> olfilmtitles = FXCollections.observableArrayList(filmtitles);

        return olfilmtitles;
    }*/

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        filmcombobox.setItems(olfilmtitles);


    }

    @FXML
    public void receipt(ActionEvent actionEvent) throws IOException{
        Window mainWindow = receiptbut.getScene().getWindow();
        Parent newRoot = FXMLLoader.load(getClass().getResource("Receiptpage.fxml"));
        mainWindow.getScene().setRoot(newRoot);

    }

    @FXML
    public void startpage(ActionEvent actionEvent) throws IOException{
        Window mainWindow = mainpagebutton.getScene().getWindow();
        Parent newRoot = FXMLLoader.load(getClass().getResource("Maincinema.fxml"));
        mainWindow.getScene().setRoot(newRoot);
    }
}

错误

    Exception in Application start method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: javafx.fxml.LoadException: 
/C:/Users/hamza/IdeaProjects/CinemaEnterprisoFinal/out/production/CinemaEnterprisoFinal/sample/FirstPage.fxml

    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.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 sample.Main.start(Main.java:13)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(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$147(WinApplication.java:177)
    ... 1 more
Caused by: java.lang.NullPointerException
    at sample.FilmsController.initialize(FilmsController.java:77)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more
Exception running application sample.Main

MainCinema.fxml代码(包含组合框(filmcombbox))

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

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ComboBox?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.HBox?>
<?import javafx.scene.text.Font?>

<AnchorPane prefHeight="518.0" prefWidth="708.0" xmlns="http://javafx.com/javafx/8.0.121" xmlns:fx="http://javafx.com/fxml/1" fx:controller="sample.FilmsController">
   <children>
      <HBox layoutX="235.0" prefHeight="129.0" prefWidth="283.0">
         <children>
            <Label prefHeight="131.0" prefWidth="285.0" text="Cinema Enterpriso">
               <font>
                  <Font name="Cambria Math" size="34.0" />
               </font>
            </Label>
         </children></HBox>
      <ComboBox fx:id="filmcombobox" layoutX="18.0" layoutY="103.0" prefHeight="36.0" prefWidth="157.0" promptText="Pick a Film" />
      <Label layoutX="22.0" layoutY="42.0" text="What's On">
         <font>
            <Font name="System Bold" size="30.0" />
         </font>
      </Label>
      <Button fx:id="snacksbut" layoutX="293.0" layoutY="367.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="84.0" text="Snacks">
         <font>
            <Font name="Cambria" size="18.0" />
         </font>
      </Button>
      <Button layoutX="293.0" layoutY="428.0" mnemonicParsing="false" prefHeight="45.0" prefWidth="84.0" text="Drinks">
         <font>
            <Font name="Cambria" size="18.0" />
         </font>
      </Button>
      <Label layoutX="14.0" layoutY="198.0" text="Price">
         <font>
            <Font name="Cambria" size="19.0" />
         </font>
      </Label>
      <TextField editable="false" layoutX="56.0" layoutY="197.0" />
      <TextField layoutX="14.0" layoutY="309.0" prefHeight="25.0" prefWidth="42.0" />
      <TextField layoutX="130.0" layoutY="309.0" prefHeight="25.0" prefWidth="42.0" />
      <Label layoutX="14.0" layoutY="259.0" prefHeight="52.0" prefWidth="104.0" text="No. of Adults">
         <font>
            <Font name="Cambria" size="17.0" />
         </font>
      </Label>
      <Label layoutX="126.0" layoutY="259.0" prefHeight="52.0" prefWidth="120.0" text="No. of Children">
         <font>
            <Font name="Cambria" size="17.0" />
         </font>
      </Label>
      <Label layoutX="22.0" layoutY="363.0" prefHeight="52.0" prefWidth="104.0" text="Total price">
         <font>
            <Font name="Cambria" size="17.0" />
         </font>
      </Label>
      <TextField editable="false" layoutX="22.0" layoutY="415.0" />
      <ComboBox layoutX="494.0" layoutY="129.0" prefHeight="36.0" prefWidth="150.0" promptText="Pick a Seat" />
      <ListView layoutX="494.0" layoutY="169.0" prefHeight="179.0" prefWidth="170.0" />
      <Button fx:id="leavepagebutton" layoutX="22.0" layoutY="468.0" mnemonicParsing="false" onAction="#leavepage" prefHeight="25.0" prefWidth="131.0" text="Back To Homepage" />
      <Button fx:id="receiptbut" layoutX="596.0" layoutY="468.0" mnemonicParsing="false" onAction="#receipt" prefHeight="25.0" prefWidth="104.0" text="Finish &amp; Pay" />
   </children>
</AnchorPane>

1 个答案:

答案 0 :(得分:0)

为新按钮添加ID:离开页面按钮

<Button fx:id="leavepagebutton" layoutX="41.0" layoutY="-114.0" mnemonicParsing="false" text="Button" />