我試圖在我的主要舞臺的矩形上單擊時顯示在新窗口上的圖像,但我收到以下錯誤:「線程中的異常」JavaFX Application Thread「java.lang.NullPointerException 「控制器之間的通信JavaFX
第一控制器(ControllerImpl)初始化第二控制器(ControllerImage),並調用它的方法(controllerImage.displayImageSel):
@FXML
private ControllerImage controllerImage = new ControllerImage();
public void rectangleSave(Rectangle r, String imagePath) {
r.setOnMousePressed((event) -> {
try {
Stage imageStage = new Stage();
FXMLLoader loader = new FXMLLoader(getClass().getResource("/images.fxml"));
Rectangle2D primScreenBounds = Screen.getPrimary().getVisualBounds();
imageStage.setX(0);
imageStage.setY(0);
imageStage.setHeight(primScreenBounds.getHeight());
imageStage.setWidth(primScreenBounds.getWidth() * 0.7);
imageStage.setTitle("JavaFX Scene Graph Demo");
Scene scene = new Scene(loader.load());
imageStage.setScene(scene);
imageStage.show();
controllerImage.displayImageSel(imagePath);
} catch (IOException e) {
System.out.println("Me cago en el PP");
}
});
}
第二控制器包含由ControllerImpl稱爲堆棧窗格和方法:
@FXML
public StackPane Spi;
public void displayImageSel(String imagePath) {
Rectangle ri = new Rectangle();
Spi.getChildren().add(ri);
Image image = new Image(new File(imagePath).toURI().toString());
ri.setFill(new ImagePattern(image));
}
當然SPI處於FXML文件中定義:
<StackPane fx:id="Spi"......
問題是ControllerImage找不到StackPane靈寶。我一直在實施我發現的與此有關的所有解決方案,但目前爲止還沒有人工作。
感謝您的幫助!
編輯:新的錯誤消息
javafx.fxml.LoadException: Root value already specified.
/C:/tutorial-app/tutorial-app/target/resources/main/images.fxml /C:/教程應用內/教程應用內/目標/資源/主/ images.fxml
at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2597)
at javafx.fxml.FXMLLoader.createElement(FXMLLoader.java:2755)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2704)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at tutorial.controller.impl.ControllerImpl.lambda$rectangleSave$1(ControllerImpl.java:146)
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.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:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$354(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
乾杯隊友,我已經應用了您的更改,現在它捕獲IOException並打印出「Me cago en el PP」。你有什麼想法來解決這個問題嗎? –
嘗試將'e.printStackTrace()'添加到'catch'子句併發布完整的堆棧跟蹤。只是'IOException'過於寬泛,但可能導致圖像路徑錯誤,或者沒有足夠的權限打開請求的文件。 – Itai
謝謝你,我編輯了錯誤的帖子 –