首先感谢您指出我在创建此帖子时遇到的错误。
我已尽可能缩小代码范围,希望现在可以更清楚了。
例如,当我尝试从现场(根)切换到另一个标签后更改标签的可见性时,发生的问题是NullPointer Exception
。这是headlineLeaderboard.setVisible(false)
。在调用该方法之前打印出headlineLeaderboard
会返回null。
即使切换进度是相同的,但我的应用程序中的每个场景都不会发生这种情况。
现在输入代码:
在主类中创建舞台,主场景等。还有一种方法可以切换场景的根。
public class Main extends Application {
private Stage primaryStage;
private Scene scene;
//
@Override
public void start(Stage primaryStage) {
this.primaryStage = primaryStage;
primaryStage.setTitle("ReaktionsTest");
try {
FXMLLoader loaderMain = new FXMLLoader(Main.class.getResource("MainWindow.fxml"));
AnchorPane paneMain = (AnchorPane) loaderMain.load();
MainWindowController mainWindowController = loaderMain.getController();
mainWindowController.setMain(this);
scene = new Scene(paneMain);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
public void setSceneLeaderboard() {
try {
Parent root = FXMLLoader.load(Main.class.getResource("LeaderboardWindow.fxml"));
scene.setRoot(root);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
在应用程序中,MainWindowController
是所有场景的控制器。 handleLeaderboardAufrufen()
首先更改场景。下一步,出现NullPointer。
public class MainWindowController {
private Main main;
@FXML private Label label1;
@FXML private Button leaderboard;
@FXML private TextField fieldName;
@FXML private Label headlineLeaderboard;
public void setMain(Main main) {
this.main = main;
}
@FXML
public void handleLeaderboardAufrufen() {
main.setSceneLeaderboard();
headlineLeaderboard.setVisible(false);
}
}
LeaderboardWindow的fxml如下所示:(在MainWindow.fxml
中使用了相同的控制器:fx:controller="application.MainWindowController"
)
<AnchorPane maxHeight="500.0" maxWidth="750.0" minHeight="500.0" minWidth="750.0" prefHeight="500.0" prefWidth="750.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.MainWindowController">
<children>
<TableView fx:id="leaderboardTable" layoutX="10.0" layoutY="92.0" prefHeight="331.0" prefWidth="729.0">
</TableView>
<Label fx:id="headlineLeaderboard" layoutX="268.0" layoutY="23.0" text="Leaderboard" textAlignment="CENTER" underline="true">
<font>
<Font name="System Bold" size="36.0" />
</font>
</Label>
</children>
</AnchorPane>
错误代码:
Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase$1.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:394)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$353(GlassViewEventHandler.java:432)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:431)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1771)
... 48 more
Caused by: java.lang.NullPointerException
at application.MainWindowController.handleLeaderboardAufrufen(MainWindowController.java:25)
... 58 more
答案 0 :(得分:0)
我同意@Sedrick的评论,您应该按照他的建议并按照准则的规定更新您的帖子。
但是,我要指出的是我正在想的一件事,如果我错了,任何人都可以随时纠正我。
您似乎正在从连接到FXML A的控制器A尝试从FXML B(ResultView)访问Text变量。
main.setSceneResult();
System.out.println("resultText " +resultText );
resultText.setText("hello");
如上面的代码所示,您将根添加到场景中(main.setSceneResult),尽管当前控制器(我假设您在其中)不知道新的父级及其成员。您可以做的是为ResultView创建一个新的控制器,我建议像这样:
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(resultView.fxml));
ResultViewController resultViewController = new ResultViewController();
loader.setController(resultViewController);
Parent root = loader.load();
然后,您可以从controllerA调用controllerB上的方法以更新Text变量。
您可以尝试使用下面的代码来验证其是否有效。
ResultViewController resultViewController = main.setSceneResult(); /*This method returns the controller which has access to the ResultText variable (if correctly setup which it looked like you had done)*/
resultViewController.setResultText("hello");
我希望这会对您有所帮助,否则我建议您使用可运行的示例来更新您的帖子:)
编辑: 我回头看了一下代码,因为我写回复时没有那么多时间。如果您有多个控制器或只有1个控制器(MainWindowController),则很难弄清楚正在发生什么。上面的示例可能会有所帮助,因此请尝试一下,但实际上,在再次查看代码后,我不确定。 另外,我不确定在fxml中指定控制器而不是像我在建议中那样将其创建并设置为加载器之后是否有原因。我也不确定您是否将“ MainWindowController”用作ResultView和当前View的控制器。如果真是这样,那么我真的建议您创建一个处理ResultView的控制器,然后让主控制器处理其余的控制器。