gpt4 book ai didi

JavaFx 节点集.....() -> NullPointer 异常

转载 作者:行者123 更新时间:2023-12-02 10:10:10 25 4
gpt4 key购买 nike

首先感谢您指出我在创建这篇文章时所犯的错误。

我尽可能缩小了代码范围,希望现在更清楚了。

因此,发生的问题是 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是所有场景的 Controller 。 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 中使用相同的 Controller :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

最佳答案

我同意@Sedrick 的评论,您应该按照他的建议和指南中的说明更新您的帖子。

但是,我会指出我认为可能是造成这种情况的一件事,如果我错了,任何人都可以随时纠正我。

您似乎从连接到 FXML A 的 Controller A 尝试从 FXML B (ResultView) 访问 Text 变量。

main.setSceneResult(); 
System.out.println("resultText " +resultText );
resultText.setText("hello");

正如您在上面的代码中所示,您将根添加到场景中(main.setSceneResult),尽管当前 Controller (我假设您位于其中)不知道新的父级及其成员。你可以做的是为 ResultView 创建一个新的 Controller ,我建议这样:

FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(resultView.fxml));
ResultViewController resultViewController = new ResultViewController();
loader.setController(resultViewController);
Parent root = loader.load();

然后,您可以从 Controller A 调用 Controller B 上的方法来更新 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");

我希望这对您有帮助,如果没有,我建议您使用可运行的示例更新您的帖子:)

编辑:我又仔细查看了代码,因为我之前写回复时没有那么多时间。如果您有多个 Controller 或者只有 1 个 Controller (MainWindowController),则很难弄清楚发生了什么。我上面的例子可能会有所帮助,所以请尝试一下,但在再次查看代码后我真的不确定。另外,我不确定您在 fxml 中指定 Controller 而不是像我在建议中那样创建 Controller 并将其设置到加载器是否有原因。我也不确定您是否使用“MainWindowController”作为 ResultView 和当前 View 的 Controller 。如果是这种情况,那么我真的建议您创建一个处理 ResultView 的 Controller ,然后让主 Controller 处理其余的事情。

关于JavaFx 节点集.....() -> NullPointer 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55064742/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com