Привет, у меня проблема с открытием нового окна fxml view после нажатия кнопки. Главное окно дисплея, на главном экране у меня шесть кнопок. Я хотел бы, чтобы я нажал кнопку открывшееся новое окно. Однако, пока я нажимаю кнопку, отображается исключение:
java.lang.reflect.InvocationTargetException
Я пробую разные решения, найденные в интернете, но отображаю это исключение. В основном я пытаюсь изменить код в моей функции переключателя, отвечающего за отображение нового представления. Я думаю, что проблема здесь или код в основном классе.
Main class:
public class Main extends Application {
public void start(Stage stage) throws Exception {
AnchorPane pane = FXMLLoader.load(getClass().getResource("/mainView.fxml"));
Scene scene = new Scene(pane);
stage.setScene(scene);
stage.show();
}
@Override
public void init() {
System.out.println("Init");
}
@Override
public void stop() {
System.out.println("Stop");
}
public static void main(String[] args) {
launch(args);
}
}
MainController.java
public class MainController {
@FXML
private Button addChangeMainButton;
@FXML
private Button viewChangesButton;
@FXML
private Button addChangeBoughtButton;
@FXML
private Button viewContractsButton;
@FXML
private Button addContractButton;
@FXML
private Button viewStatisticsButton;
@FXML
void chooseViewAction(ActionEvent event) throws IOException {
Button button = (Button) event.getSource();
String buttonId = button.getId();
MainLogic mainl = new MainLogic();
mainl.chooseView(buttonId);
}
}
public class MainLogic {
private String chooseView;
public void chooseView(String chooseView) throws IOException {
switch (chooseView) {
case "addChangeMainButton":
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("addChangeVIEW.fxml"));
Parent root1 = fxmlLoader.load();
Stage stage = new Stage();
stage.setScene(new Scene(root1));
stage.show();
break;
case "viewChangesButton":
System.out.println("viewChangesButton");
break;
case "addChangeBoughtButton":
System.out.println("addChangeBoughtButton");
break;
case "viewContractsButton":
System.out.println("viewContractsButton");
break;
case "addContractButton":
System.out.println("addContractButton");
break;
case "viewStatisticsButton":
System.out.println("viewStatisticsButton");
break;
default:
System.out.println("default");
break;
}
}
}
mainView.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.MainController">
<children>
<Button fx:id="addChangeMainButton" layoutX="31.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Change" />
<Button fx:id="viewChangesButton" layoutX="31.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Changes" />
<Button fx:id="addChangeBoughtButton" layoutX="217.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Change Bought/Sold" />
<Button fx:id="viewContractsButton" layoutX="225.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Contracts" />
<Button fx:id="addContractButton" layoutX="403.0" layoutY="45.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="Add Contract" />
<Button fx:id="viewStatisticsButton" layoutX="403.0" layoutY="283.0" mnemonicParsing="false" onAction="#chooseViewAction" prefHeight="70.0" prefWidth="150.0" text="View Statistics" />
</children>
</AnchorPane>
addChangeVIEW.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/10.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.controller.addChangeController">
<children>
<Button fx:id="button" layoutX="263.0" layoutY="210.0" mnemonicParsing="false" text="Button" />
</children>
</AnchorPane>
addChangeController.java
package com.controller;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
public class addChangeController {
@FXML
private Button button;
}
module-info.java
module ProjectCPApril {
requires javafx.graphics;
requires javafx.fxml;
requires javafx.controls;
exports com.controller to javafx.graphics;
opens com.controller to javafx.fxml;
exports com.logic to javafx.graphics;
opens com.logic to javafx.fxml;
}
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1787)
at javafx.fxml/javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1670)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Node.fireEvent(Node.java:8879)
at javafx.controls/javafx.scene.control.Button.fire(Button.java:200)
at javafx.controls/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:206)
at javafx.controls/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at javafx.base/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at javafx.base/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at javafx.base/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at javafx.base/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at javafx.base/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at javafx.base/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at javafx.base/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.base/javafx.event.Event.fireEvent(Event.java:198)
at javafx.graphics/javafx.scene.Scene$MouseHandler.process(Scene.java:3851)
at javafx.graphics/javafx.scene.Scene$MouseHandler.access$1200(Scene.java:3579)
at javafx.graphics/javafx.scene.Scene.processMouseEvent(Scene.java:1849)
at javafx.graphics/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2588)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:397)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:434)
at javafx.graphics/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:390)
at javafx.graphics/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:433)
at javafx.graphics/com.sun.glass.ui.View.handleMouseEvent(View.java:556)
at javafx.graphics/com.sun.glass.ui.View.notifyMouse(View.java:942)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:174)
at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.reflect.InvocationTargetException
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at com.sun.javafx.reflect.Trampoline.invoke(MethodUtil.java:76)
at jdk.internal.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at javafx.base/com.sun.javafx.reflect.MethodUtil.invoke(MethodUtil.java:273)
at javafx.fxml/com.sun.javafx.fxml.MethodHelper.invoke(MethodHelper.java:83)
at javafx.fxml/javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1782)
... 47 more
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
at javafx.fxml/javafx.fxml.FXMLLoader.load(FXMLLoader.java:2435)
at ProjectCPApril/com.logic.MainLogic.chooseView(MainLogic.java:27)
at ProjectCPApril/com.controller.MainController.chooseViewAction(MainController.java:43)
... 58 more
Stop
Process finished with exit code 0