java.lang.NullPointerException 处理 2 个控制器之间的控件 - Javafx [英] java.lang.NullPointerException in handling with controls between 2 controllers - Javafx
问题描述
我一直在 Javafx 中的 2 个场景之间尝试按钮事件.
I've been trying button events between 2 scenes in Javafx.
这是 Start.fxml(在视图包中):
This is Start.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.StartController">
<children>
<Label fx:id="lbl1" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt1" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn1" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn1Click" text="Click" />
</children>
</AnchorPane>
这是 Preserences.fxml(在视图包中):
This is Preserences.fxml (in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="292.0" prefWidth="383.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.PreferencesController">
<children>
<Label fx:id="lbl2" layoutX="87.0" layoutY="60.0" prefHeight="17.0" prefWidth="209.0" />
<TextField fx:id="txt2" layoutX="87.0" layoutY="121.0" prefHeight="25.0" prefWidth="209.0" />
<Button fx:id="btn2" layoutX="171.0" layoutY="191.0" mnemonicParsing="false" onAction="#btn2Click" text="Click" />
</children>
</AnchorPane>
这是 Main.fxml,它就像是前两个文件的容器.(在视图包中):
This is Main.fxml which is like a container for the previous 2 files.(in view package):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefHeight="481.0" prefWidth="468.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="controller.MainController">
<children>
<TabPane layoutX="14.0" layoutY="14.0" prefHeight="481.0" prefWidth="468.0" tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab closable="false" text="Start">
<content>
<fx:include source="Start.fxml" />
</content>
</Tab>
<Tab closable="false" text="Preferences">
<content>
<fx:include source="Preferences.fxml" />
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
这是 StartController.java(在控制器包中):
This is StartController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class StartController implements Initializable {
@FXML public static Label lbl1;
@FXML public static TextField txt1;
@FXML public static Button btn1;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML private void btn1Click(ActionEvent e){
System.out.println("Button 1 clicked");
lbl1.setText(PreferencesController.txt2.getText());
}
}
这是 PreferencesController.java(在控制器包中):
This is PreferencesController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
public class PreferencesController implements Initializable {
@FXML public static Label lbl2;
@FXML public static TextField txt2;
@FXML public static Button btn2;
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@FXML private void btn2Click(ActionEvent e){
System.out.println("Button 2 clicked");
lbl2.setText(StartController.txt1.getText());
}
}
这是 MainController.java(在控制器包中):
This is MainController.java (in controller package):
package controller;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.Initializable;
public class MainController implements Initializable{
@Override
public void initialize(URL arg0, ResourceBundle arg1) {
}
}
最后,这是应用程序包中的 Main.java:
Finally, this is Main.java in application package:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("/view/Main.fxml"));
Scene scene = new Scene(root);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
但是当我点击按钮时,它给了我错误:
But when I clicked the buttons, it gives me the errors:
"Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException...
...
Caused by: java.lang.NullPointerException
at controller.StartController.btn1Click(StartController.java:26)
... 61 more"
请帮帮我!:(
推荐答案
OK 所以 NPE 被抛出到这一行:
OK so the NPE is being thrown on this line:
lbl1.setText(PreferencesController.txt2.getText());
在基本的 Java 级别,只有两种方法可以实现这一点:
At the basic Java level, are only two ways that you could get that to happen:
- 当
lnl1
为null
时,或 - 当
PreferencesController.txt2
为null
时.
- when
lnl1
isnull
, or - when
PreferencesController.txt2
isnull
.
没有其他可能的解释......鉴于 PreferencesController.txt2
是一个静态字段.
No other explanations are possible ... given that PreferencesController.txt2
is a static field.
我不熟悉 JavaFX 或 FXML,但我怀疑您的错误是将字段声明为 static
.我怀疑这会导致 FXML 机制无法正确注入对 UI 组件的引用.所以这些变量保持为 null
... 你得到一个 NPE.
I'm not familiar with JavaFX or FXML, but I suspect that your mistake is declaring the fields as static
. I suspect that this causes the FXML mechanisms to not inject the reference to the UI component correctly. So those variables remain as null
... and you get an NPE.
确认信息如下:https://stackoverflow.com/a/23109125/139985.(感谢@James_D 的链接)
Confirmation is here: https://stackoverflow.com/a/23109125/139985. (Thanks for the link @James_D)
这篇关于java.lang.NullPointerException 处理 2 个控制器之间的控件 - Javafx的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!