I got a problem that I absolutely can't solve on my own because I have just started using JAVA FX. I get a nasty javafx.fxml.LoadException: , but I have done exactly like a guide, I can get my main to run, but when I try changing .fxml files via button_signup for a new scene, I get javafx.fxml.LoadException:.
javafx.fxml.LoadException:
/C:/Users/avsti/IdeaProjects/Assignment2/out/production/Assignment2/application/resources/register.fxml
at [email protected]/javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2707)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2685)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3331)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3287)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3255)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3227)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3203)
at [email protected]/javafx.fxml.FXMLLoader.load(FXMLLoader.java:3196)
at application.Utils.changeScene(Utils.java:17)
at application.Controller$1.handle(Controller.java:22)
at application.Controller$1.handle(Controller.java:19)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Node.fireEvent(Node.java:8797)
at [email protected]/javafx.scene.control.Button.fire(Button.java:203)
at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Button.setOnAction(javafx.event.EventHandler)" because "this.button_signup" is null
at application.Controller.initialize(Controller.java:19)
at [email protected]/javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2655)
... 55 more
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Root cannot be null
at [email protected]/javafx.scene.Scene.<init>(Scene.java:348)
at [email protected]/javafx.scene.Scene.<init>(Scene.java:210)
at application.Utils.changeScene(Utils.java:23)
at application.Controller$1.handle(Controller.java:22)
at application.Controller$1.handle(Controller.java:19)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:49)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Node.fireEvent(Node.java:8797)
at [email protected]/javafx.scene.control.Button.fire(Button.java:203)
at [email protected]/com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:208)
at [email protected]/com.sun.javafx.scene.control.inputmap.InputMap.handle(InputMap.java:274)
at [email protected]/com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:247)
at [email protected]/com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:234)
at [email protected]/com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at [email protected]/com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at [email protected]/com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at [email protected]/com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at [email protected]/com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at [email protected]/javafx.event.Event.fireEvent(Event.java:198)
at [email protected]/javafx.scene.Scene$MouseHandler.process(Scene.java:3881)
at [email protected]/javafx.scene.Scene.processMouseEvent(Scene.java:1874)
at [email protected]/javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2607)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:411)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:301)
at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$2(GlassViewEventHandler.java:450)
at [email protected]/com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:424)
at [email protected]/com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:449)
at [email protected]/com.sun.glass.ui.View.handleMouseEvent(View.java:551)
at [email protected]/com.sun.glass.ui.View.notifyMouse(View.java:937)
at [email protected]/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at [email protected]/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
at java.base/java.lang.Thread.run(Thread.java:833)
Main.java
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.IOException;
public class Main extends Application{
@Override
public void start(Stage stage) throws IOException {
FXMLLoader fxmlLoader = new FXMLLoader(MyHealth.class.getResource("resources/login.fxml"));
Scene scene = new Scene(fxmlLoader.load());
stage.setTitle("MyHealth");
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
}
Controller.java
package application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import java.net.URL;
import java.util.ResourceBundle;
public class Controller implements Initializable {
@FXML
private Button button_signup;
@Override
public void initialize(URL url, ResourceBundle resources) {
button_signup.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
Utils.changeScene(event, "resources/register.fxml", "Signup!");
}
});
}
}
Utils.java
package application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.event.ActionEvent;
import java.io.IOException;
public class Utils {
public static void changeScene(ActionEvent event, String fxmlFile, String title) {
Parent root = null;
try {
root = FXMLLoader.load(Utils.class.getResource(fxmlFile));
} catch (IOException e) {
e.printStackTrace();
}
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setTitle(title);
stage.setScene(new Scene(root));
stage.show();
}
}
register.fxml
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.Cursor?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/18" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.Controller">
<children>
<Label layoutX="181.0" layoutY="103.0" text="Username:">
<font>
<Font size="16.0" />
</font>
</Label>
<Label layoutX="183.0" layoutY="138.0" text="Password:">
<font>
<Font size="16.0" />
</font>
</Label>
<TextField fx:id="signup_username" layoutX="330.0" layoutY="103.0" />
<TextField fx:id="signup_password" layoutX="331.0" layoutY="138.0" />
<TextField fx:id="signup_firstname" layoutX="331.0" layoutY="175.0" />
<TextField fx:id="signup_lastname" layoutX="331.0" layoutY="209.0" />
<Label layoutX="178.0" layoutY="175.0" text="First Name:">
<font>
<Font size="16.0" />
</font>
</Label>
<Label layoutX="180.0" layoutY="209.0" text="Last Name:">
<font>
<Font size="16.0" />
</font>
</Label>
<Label layoutX="218.0" layoutY="338.0" text="Already a member?">
<font>
<Font name="System Bold" size="12.0" />
</font>
</Label>
<Button fx:id="button_register" layoutX="270.0" layoutY="255.0" mnemonicParsing="false" text="Register">
<cursor>
<Cursor fx:constant="HAND" />
</cursor></Button>
<Button fx:id="button_return" layoutX="327.0" layoutY="334.0" mnemonicParsing="false" style="-fx-background-color: transparent;" text="Login!" textFill="#0f19e1" underline="true">
<font>
<Font name="System Bold" size="12.0" />
</font>
<cursor>
<Cursor fx:constant="HAND" />
</cursor></Button>
</children>
<cursor>
<Cursor fx:constant="HAND" />
</cursor>
</AnchorPane>
CodePudding user response:
"Caused by: java.lang.NullPointerException: Cannot invoke "javafx.scene.control.Button.setOnAction(javafx.event.EventHandler)" because "this.button_signup" is null "
tells me that your button is null
try initializing it
private Button button_signup = new button();