JavaFX在main中加载异常

时间:2014-12-12 20:27:16

标签: javafx

我的代码存在问题我正在为考试项目写作。它在JavaFX中。 我将一些参数从我的模型发送到我的主,然后再从主模块发送到控制器。即使位置集应该正确,我也会收到LoadException。 这是错误代码:

javafx.fxml.LoadException: 生物/ grpro_project /目标/类/项目/视图/ reservation.fxml

at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2595)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2565)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2435)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at project.ReservationMain.showReservationView(ReservationMain.java:22)
at project.ReservationMain.start(ReservationMain.java:37)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/272422922.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/2050891229.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1063369253.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/1170609517.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/1399457240.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: 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:483)
at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:483)
at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2560)
... 18 more
Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at project.controller.ReservationController.initialize(ReservationController.java:166)
... 28 more

这是主要课程: 公共类ReservationMain扩展了Application {

public void showReservationView(Stage stage, String showtitle, String showdate, String showtime) {
    try {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("view/reservation.fxml"));
        Parent reservationView = loader.load();
        ReservationController controller = loader.<ReservationController>getController();
        controller.setup(showtitle, showdate, showtime);
        stage.setTitle("Reservation");
        stage.setScene(new Scene(reservationView, 800, 600));

        stage.show();
    } catch (IOException e) {
        e.printStackTrace(); //Show a dialog or something... ?
    }

}

@Override
public void start(Stage primaryStage) throws Exception {
    showReservationView(primaryStage, "", "", "");
}

public static void main(String[] args) {
    launch(args);
}

}

这是用于将参数发送到main的模型中的方法:

    reservationButton.setOnAction((event) -> {
        System.out.println(showtime + showdate + showtime);
        ReservationMain newscene = new ReservationMain();
        Stage stage = new Stage();
        newscene.showReservationView(stage, showtitle, showdate, showtime);
    });

希望有人可以提供帮助 - 我现在已经尝试解决这个问题很长时间了。

控制器代码(从模型类获取数据,但不应该是一个问题):

public class ReservationController {

private Reservation reservation = new Reservation();
private DatabaseLoad databaseLoad = new DatabaseLoad();
private DatabaseInsert databaseInsert = new DatabaseInsert();
private int theater;
private ObservableList numberOfSeatsSelectedItems = FXCollections.observableArrayList();
private int numberOfSeatsToSelect = 1;
private int selectedRow;
private int selectedSeat;
private int showID;


@FXML
private TextField nameField;
@FXML
private TextField phoneField;
@FXML
private GridPane seatsGridContainer;
@FXML
private VBox movieScreenBox;
@FXML
private Text theaterNumber;
@FXML
private Text freeSeats;
@FXML
private ComboBox seatsSelectedBox;
@FXML
private Button reserveButton;
@FXML
private Text movieTitleText;
@FXML
private Text movieDateText;
@FXML
private Text movieTimeText;


public ReservationController() {

}

public void setup(String showtitle, String showdate, String showtime){
    showID = reservation.loadTheatertoShow(showtitle, showdate, showtime);
    reservation.loadTheaterFromDatabase(showID);
    theater = reservation.getTheaterNumber();
    numberOfSeatsSelectedItems.addAll(1,2,3,4,5,6,7,8);
}

@FXML
public void initialize() {
    //int numberOfSeatsSelected;
    seatsSelectedBox.setItems(numberOfSeatsSelectedItems);
    seatsSelectedBox.setOnAction((event) -> {
        numberOfSeatsToSelect = (int) seatsSelectedBox.getSelectionModel().getSelectedItem();
    });
    movieScreenBox.setBackground(new Background(new BackgroundFill(Color.GRAY, CornerRadii.EMPTY, Insets.EMPTY)));

    seatsGridContainer.setHgap(8);
    seatsGridContainer.setVgap(8);
    seatsGridContainer.setAlignment(Pos.CENTER);
    ArrayList occupiedSeats = reservation.loadOccupiedSeats(showID);

    Seat[][] seat = new Seat[reservation.getNumberOfRows()][reservation.getNumberOfSeatsInARow()];
    for (int row = 0; row < seat.length; row++) {
        for (int seatNumber = 0; seatNumber < seat[0].length; seatNumber++) {
            seat[row][seatNumber] = new Seat("");
            seat[row][seatNumber].setPrefSize(30, 30);

            //
            final int finalRow = row;
            final int finalSeatNumber = seatNumber;
            seat[row][seatNumber].setOnMouseClicked(arg0 -> {
                selectedRow = finalRow;
                selectedSeat = finalSeatNumber;
                for (int i = 0; i < seat.length; i++) {
                    for (int j = 0; j < seat[0].length; j++) {
                        if (seat[i][j].isFree()) {
                            seat[i][j].setStyle("-fx-background-color: green;");
                            seat[i][j].setSelected(false);
                        }
                    }
                }
                // check: does the seat selection extend the boundaries of the theater or go into already booked seats?
                if(seat[finalRow].length <= finalSeatNumber-1 + numberOfSeatsToSelect) {
                    int seatsLeft = seat[finalRow].length - finalSeatNumber;
                    seatsSelectedBox.setValue(seatsLeft);
                    for(int i = 0 ; i < seatsLeft ; i++) {
                        seat[finalRow][finalSeatNumber + i].setStyle("-fx-background-color: dodgerblue;");
                    }
                }
                else {
                    for(int i = 0 ; i < numberOfSeatsToSelect ; i++) {
                        if(!seat[finalRow][finalSeatNumber + i].isFree()) {
                            seatsSelectedBox.setValue(i);
                            break;
                        }
                        seat[finalRow][finalSeatNumber + i].setStyle("-fx-background-color: dodgerblue;");
                        seat[finalRow][finalSeatNumber + i].setSelected(true);
                    }
                }
            });

            // checks and sets which seats are booked
            if(occupiedSeats.contains("" + row + seatNumber)) {
                seat[row][seatNumber].setIsFree(false);
            }

            //set free seats to change the cursor on mouseover
            if (seat[row][seatNumber].isFree()) {
                seat[row][seatNumber].setStyle("-fx-background-color: green;");
                seat[row][seatNumber].setOnMouseEntered(me -> seatsGridContainer.setCursor(Cursor.HAND));
                seat[row][seatNumber].setOnMouseExited(me -> seatsGridContainer.setCursor(Cursor.DEFAULT));
            }
            // set booked seats to display as red
            else {
                seat[row][seatNumber].setStyle("-fx-background-color: red;");
                seat[row][seatNumber].setDisable(true);
            }
            seatsGridContainer.add(seat[row][seatNumber], seatNumber, row);
        }
    }


    reserveButton.setOnAction((event) -> {
        databaseInsert.newPersonInDatabase(nameField.getText(),phoneField.getText());
        ArrayList nextPersonIDs = databaseLoad.getFromDatabase(
                "SELECT * FROM person WHERE PhoneNumber=" + phoneField.getText(),"person")[2];
        int nextPersonID = (int) nextPersonIDs.get(nextPersonIDs.size() - 1);

        ArrayList arr = databaseLoad.getFromDatabase("SELECT * FROM reservations","reservations")[0];
        int nextReservationID = (int) arr.get(arr.size()-1) + 1;


        for(int i = 0; i < numberOfSeatsToSelect ; i++) {
            databaseInsert.newReservationInDatabase(
                    nextReservationID, nextPersonID, showID, selectedRow, selectedSeat+i);
        }
        initialize();
    });

    ArrayList[] shows = databaseLoad.getFromDatabase("SELECT * FROM shows WHERE ShowID=" + showID, "shows");
    movieTitleText.setText("Film: " + shows[0].get(0));
    movieDateText.setText("Dato: " + shows[1].get(0).toString());
    movieTimeText.setText("Tidspunkt:  " + shows[2].get(0).toString());

    theaterNumber.setText("Sal nr: " + theater);
    freeSeats.setText("Ledige sæder: " + (reservation.getNumberOfSeats()-occupiedSeats.size()));
}

}

如果我尝试将f添加到fxml文件的文件夹(/view/reservation.fxml),则会出现以下错误消息:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:875)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(LauncherImpl.java:157)
at com.sun.javafx.application.LauncherImpl$$Lambda$1/245257410.run(Unknown Source)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2428)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2403)
at project.ReservationMain.showReservationView(ReservationMain.java:22)
at project.ReservationMain.start(ReservationMain.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(LauncherImpl.java:821)
at com.sun.javafx.application.LauncherImpl$$Lambda$50/2038127331.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(PlatformImpl.java:323)
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1716030070.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$164(PlatformImpl.java:292)
at com.sun.javafx.application.PlatformImpl$$Lambda$48/1391275142.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(PlatformImpl.java:291)
at com.sun.javafx.application.PlatformImpl$$Lambda$47/156840264.run(Unknown Source)
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$141(WinApplication.java:102)
at com.sun.glass.ui.win.WinApplication$$Lambda$38/848186220.run(Unknown Source)
... 1 more

使用退出代码1完成处理

0 个答案:

没有答案