【发布时间】:2019-04-30 07:18:57
【问题描述】:
这是一个非常、非常奇怪和具体的情况。我做了一个最小的可验证示例:
文件:Main.groovy
public class Main extends Application {
public static void main(String[] args) {
launch(Main,args);
}
@Override
public void start(Stage primaryStage) {
Button destroy = new Button("Break the program")
Button openWindow= new Button("Open new Window")
destroy.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
env.put(Context.PROVIDER_URL, "[IP censored]");
InitialContext ic = new InitialContext(env);
}
});
openWindow.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent e) {
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getClassLoader().getResource("test.fxml"))
TestController testController =new TestController()
loader.setController(testController)
Parent testWindowRoot = loader.load();
Scene scene = new Scene(testWindowRoot);
Stage stage = new Stage()
stage.setTitle("Test");
stage.setScene(scene);
stage.show();
}
});
HBox hbox = new HBox(destroy,openWindow)
StackPane root = new StackPane();
root.setPadding(new Insets(5));
root.getChildren().add(hbox);
primaryStage.setTitle("JavaFX Test");
primaryStage.setScene(new Scene(root, 300, 150));
primaryStage.show();
}
}
文件:TestController.groovy
class TestController implements Initializable {
@FXML AnchorPane mainAnchorPane
@FXML Label label
public void initialize(URL arg0, ResourceBundle arg1) {
label.setText("test 2") //this is line 20
}
}
文件:test.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane fx:id="mainAnchorPane" maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.171" xmlns:fx="http://javafx.com/fxml/1">
<children>
<Label fx:id="label" layoutX="240.0" layoutY="192.0" text="Test 1" />
</children>
</AnchorPane>
所以这是一个非常基本的例子来重现所发生的事情。当我单击 Open Window 按钮时,我看到一个带有文本 Test 2 的新窗口,因为在 TestController 的 initialize() 方法中有对 setText("Test 2") 的调用。然后,当我单击“中断程序”时,它会创建一个映射并使用 JMS 信息填充它(对不起,我无法为您提供 JMS 队列的 IP)并实例化 InitialContext。之后,任何单击“打开窗口”按钮都会使程序崩溃。没有其他窗口打开过。我看到以下堆栈跟踪:
java.lang.NullPointerException: Cannot invoke method setText() on null object
[...]
at TestController.initialize(TestController.groovy:20)
这当然不是真的,因为在我尝试连接到 WebLogic 之前,他打开那个窗口从来没有遇到过问题。
注意:这只发生在我从 Groovy 中执行 JavaFX 时,而不是从 Java 中,这几个月来一直完美地为我工作。
那么,交易是什么?这两件事之间有什么联系?为什么初始化InitialContext后打不开FXML窗口?
使用 jdk1.8.0_181
编辑:我找到了解决方法!它只是解决了症状。问题是 GUI 对象在使用 WL 后被神奇地设置为 null。好吧,我可以使用lookup() 方法来“刷新”对象引用。这显然不是完美的解决方案,但我的代码现在可以工作了。不过,我仍然对这个问题的真正答案非常好奇。
【问题讨论】:
标签: java javafx groovy jms weblogic