0

So, I have an application with a few fxml files. One of the fxml creates a scene that is a different size than all the others, and I would like for it to load the window to match the scene size.

What I have done is referenced the stage in my HumansightExecutiveController.java class with Stage stage = (Stage) humansightExecutivePane.getScene().getWindow(); in my initialize function for said controller class. Also in this initialize function is stage.sizeToScene, which produces a NullPointerException error. I'm unsure as to why this is happening as the previous line getting the window is does not produce an error.

My code:

HumansightExecutiveController.java

package application;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;

public class HumansightExecutiveController implements Initializable{

    @FXML 
    private AnchorPane humansightExecutivePane;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        Stage stage = (Stage) humansightExecutivePane.getScene().getWindow();
        stage.sizeToScene();
    }
}

Main.java

package application;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {

            // Load FXML file
            Parent root = FXMLLoader.load(getClass().getResource("ExecutivePromptXML.fxml"));

            // Create scene
            Scene scene = new Scene(root, 400, 600);

            // Load CSS
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());

            // Display application
            primaryStage.setScene(scene);
            primaryStage.setResizable(false);
            primaryStage.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }
}

ExecutiveLoginController.java

package application;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.layout.AnchorPane;

public class ExecutiveLoginController implements Initializable{

    @FXML
    private AnchorPane loginPane;

    public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub

    }

    @FXML
    private void loginReturn(ActionEvent event) throws IOException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("ExecutivePromptXML.fxml"));
        loginPane.getChildren().setAll(pane);
    }

    @FXML
    private void authenticate(ActionEvent event) throws IOException {
        AnchorPane pane = FXMLLoader.load(getClass().getResource("HumansightExecutiveXML.fxml"));
        loginPane.getChildren().setAll(pane);
    }
}

Error:

The three warnings at the top happen even when the application is working fine. They have always happened, so I don't think they are relevant.

Jan 27, 2019 3:55:20 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 10.0.1 by JavaFX runtime of version 8.0.191
Jan 27, 2019 3:55:22 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 10.0.1 by JavaFX runtime of version 8.0.191
Jan 27, 2019 3:55:23 PM javafx.fxml.FXMLLoader$ValueElement processValue
WARNING: Loading FXML document with JavaFX API of version 10.0.1 by JavaFX runtime of version 8.0.191
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)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 45 more
Caused by: javafx.fxml.LoadException: 
/Users/student/Desktop/Humansight/EclipseEnvironment/humansightExecutiveFirstDraft/bin/application/HumansightExecutiveXML.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.ExecutiveLoginController.authenticate(ExecutiveLoginController.java:32)
    ... 55 more
Caused by: java.lang.NullPointerException
    at application.HumansightExecutiveController.initialize(HumansightExecutiveController.java:18)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 63 more

I would like to clarify that my question is not about what a NullPointerException is. My question is why my stage variable is null despite getting no NPE when initializing it.

Let me know if any more information is needed and thank you.

Riley Fitzpatrick
  • 869
  • 2
  • 16
  • 38
  • My guess is that your code is "pointing" to something that is "null" (ie: doesn't exist). Is your `stage` or its `Scene` null? – Zephyr Jan 27 '19 at 21:32
  • @Zephyr That was the first thing I checked and that's what my question is asking. I understand that I am looking for why my stage is null. Based on the fact that the `Stage stage = ...` line ran successfully, the only way that stage could be null is if somehow `getWindow()` returned null, but I would still expect an error if that were the case. I am pretty sure `Scene` is not null, because it is in the `initialize()` function, which should mean that the scene is fully loaded by the time the method runs. – Riley Fitzpatrick Jan 27 '19 at 21:45
  • 1
    `getWindow()` could successfully return `null` if the stage exists but the `Window` has not been rendered/displayed yet. Try adding the method call in a `Platform.runLater()` block; you're calling it from the `initialize()` method, which is run before the stage is shown, therefore, there is no `Window` yet. – Zephyr Jan 27 '19 at 21:48
  • @Zephyr I will look deeper into `getWindow()` then. The fxml does not render/display, but I think (might be wrong) that putting things in the `initialize()` function should mean the `Scene` is created by the time the method is called. – Riley Fitzpatrick Jan 27 '19 at 21:53
  • 1
    The `Scene` is created, yes. But the `Window` is not, because there's no window being displayed yet :) – Zephyr Jan 27 '19 at 21:59
  • @Zephyr Interesting. Any ideas why that might happen? – Riley Fitzpatrick Jan 27 '19 at 22:03
  • That is just how JavaFX works. – Zephyr Jan 27 '19 at 22:07
  • Counting the lines in the code you posted, the line causing the NPE is `Stage stage = (Stage) humansightExecutivePane.getScene().getWindow();` not the following line. The `Scene` is not even created when `FXMLLoader.load` completes which happens after `initialize` is called for the controller. – fabian Jan 27 '19 at 22:10
  • @fabian Oops, I will edit my post to show the code that loads this scene. There is a scene the user goes through before this in which a button loads the new fxml. The method is the one called `authenticate()`. Thanks. – Riley Fitzpatrick Jan 27 '19 at 22:13
  • @Zephyr The `Platform.runLater()` method worked! Thank you! If you want the reputation you can type it out as an official answer and I will click the accept button. – Riley Fitzpatrick Jan 27 '19 at 22:19
  • 1
    Not necessary, as there are already good answers on this elsewhere :) – Zephyr Jan 27 '19 at 22:26

0 Answers0