嗨,点击按钮后,我在打开新窗口fxml视图时遇到问题。主窗口显示,在主视图中,我有六个按钮。我想点击按钮打开新窗口。但是,当我单击按钮时,显示异常:
java.lang.reflect.InvocationTargetException
我尝试在Internet中找到其他解决方案,但显示此异常。主要是我尝试在我的切换功能中修改代码,以显示新视图。我认为问题出在这里或主类中的代码。
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
答案 0 :(得分:1)
将评论中的信息复制到答案中
堆栈跟踪的重要部分是:
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
这告诉您调用FXMLLoader
时null
的位置是load
。当您通过Class.getResource
进行设置时,表示该方法正在返回null
。当getResource
返回null
时,这意味着使用给定的路径找不到资源。
您当前使用"addChangeVIEW"
作为路径,但是您说:
addChangeVIEW.fxml
在文件夹resources
中,MainLogic.java
在软件包com.logic
中
由于您的路径没有前导/
,因此它与您调用Class
的{{1}}有关。由于您使用getResource
,因此路径将解析为MainLogic.class
-这不是您拥有资源的地方。您已将资源放在类路径的根部,可以使用绝对路径com/logic/addChangeVIEW.fxml
来检索该资源。
有关更多信息,请参见Class.getResource(String)
。