Issue
trying set image to button, but receive NullPointerException.
structure:
resources
com.example.test
images
file_open_icon.png
and code:
openImageview.setImage(new Image(Objects.requireNonNull(getClass().getResourceAsStream("images/file_open_icon.png"))));
Button:
<ToolBar prefHeight="40.0" prefWidth="200.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="25.0">
<Button fx:id="toolbarHandleOpen" mnemonicParsing="false" onAction="#handle_open">
<graphic>
<ImageView fx:id="openImageview" fitHeight="23.0" fitWidth="23.0" pickOnBounds="true" preserveRatio="true" />
</graphic>
</Button>
</ToolBar>
Solution
I had tried to set the picture in the controller constructor, which caused the null pointer exception because the ImageView was not initialized at that time.
I moved the call to set the image to the controller initialize() method and it worked fine.
Answered By - Енот Полоскун
Answer Checked By - Willingham (JavaFixing Volunteer)