0

I have been trying to access the imageview in JavaFXML Controller. I have set the imageview fx:id = BWBHQ in the scene builder.

And I'm trying to access it so-

image= new javafx.scene.image.Image(getClass().getResource("dragon.png").toExternalForm());

System.out.println(image.getHeight());//Image is getting loaded

BWBHQ.setImage(image);//null pointer error

I cant figure out why there is a null pointer error.

Please help!!

This is my Application Class:

public class JavaFXApplication2 extends Application {
    public static Stage mainStage;
    public static Scene scene1,scene2,scene3,scene4;
    @Override
    public void start(Stage stage) throws Exception {

        Parent root2 = FXMLLoader.load(getClass().getResource("InputScreen.fxml"));
        scene2 = new Scene(root2);

         Parent root4 = FXMLLoader.load(getClass().getResource("GameScreen.fxml"));
        scene4 = new Scene(root4);


        mainStage=stage;

        stage.setScene(scene1);
        stage.show();
    }

}

This is my FXMLController class

public class FXMLDocumentController implements Initializable {

    @FXML 
    public ImageView BWBHQ  ;

    @FXML
    public void handleInputScreenButtonAction(ActionEvent event) {
      initializeWorld();
      JavaFXApplication2.mainStage.setScene(JavaFXApplication2.scene4);
    }

    public void initializeWorld()
    {
        //create cities
        javafx.scene.image.Image image;
        image= new javafx.scene.image.Image(getClass().getResource("dragon.png").toExternalForm());
        System.out.println(image.getHeight());
        BWBHQ.setImage(image);
    }
}

This is the GameScreen.fxml that contains the imageview BWBHQ

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.image.*?>
<?import javafx.scene.text.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="463.0" prefWidth="800.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="javafxapplication2.FXMLDocumentController">
   <children>
      <ImageView fx:id="BWBHQ" fitHeight="25.0" fitWidth="25.0" layoutX="722.0" layoutY="190.0" pickOnBounds="true" preserveRatio="true" />
      <ImageView fx:id="RW1BHQ" fitHeight="25.0" fitWidth="25.0" layoutX="674.0" layoutY="190.0" pickOnBounds="true" preserveRatio="true" />
   </children>
</AnchorPane>

This is the error

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:8413)
    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:381)
    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$354(GlassViewEventHandler.java:417)
    at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
    at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:416)
    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: java.lang.NullPointerException
    at javafxapplication2.FXMLDocumentController.initializeWorld(FXMLDocumentController.java:118)
    at javafxapplication2.FXMLDocumentController.handleInputScreenButtonAction(FXMLDocumentController.java:99)
    ... 55 more
  • Have you added a `@FXML` annotation for the variable? – Courage May 05 '17 at 09:50
  • Hi Oswald, yes i have put the @FXML tag. The same issue happens when i try to settext for a label. – Sheetal Borar May 05 '17 at 10:05
  • Check if your Java class is properly linked to the FXML file – Courage May 05 '17 at 10:09
  • Maybe 1) You are creating the controller manually, not by using the `FXMLLoader` to load the file and let it to create the controller for you; 2) You are recreating the controllers in the `initialize` method like `BWBHQ = new ImageView()`. Everything is just guessing until you dont share your code ... – DVarga May 05 '17 at 10:10
  • So I have linked the controller with the FXML File, by setting the controller class in Scene builder and that class is the same as my controller. Code for fxml loader: Parent root = FXMLLoader.load(getClass().getResource("MainScreen.fxml")); scene1 = new Scene(root); and im not recreating by new ImageView() – Sheetal Borar May 05 '17 at 10:16
  • @SheetalBorar please post all the relevant code, i.e your FXML and the controller – Courage May 05 '17 at 10:26
  • Hi, I have edited the question with all the relevant code – Sheetal Borar May 05 '17 at 12:05
  • 1
    You're using different controller instances for the fxml files. Suitable approaches should be described here: http://stackoverflow.com/questions/14187963/passing-parameters-javafx-fxml – fabian May 05 '17 at 12:31
  • I realized I was calling the imageview in the button click event of a button which is not on the same FXML as the imageview, hence the null pointer error. Thanks for your help everyone :) – Sheetal Borar May 06 '17 at 01:26

0 Answers0