I've done some research on the subject and still stuck. I've also searched other ways of doing it here in stack-overflow but no luck so far. I've tried without sample, moving it to another folder, nothing worked. Here's the Structure:
Main Java Code
package sample;
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) throws Exception{
Parent root=FXMLLoader.load(getClass().getResource("sample/ui.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}
Controller.java code
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.text.Text;
public class Controller {
@FXML
private Text output;
@FXML
private void processNumpad(ActionEvent event){
}
@FXML
private void processOperator(ActionEvent event){
}
}
ui.fxml code
<?xml version="1.0" encoding="UTF-8"?>
<?import java.net.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.beans.property.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.shape.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.image.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>
<?import javafx.scene.paint.Color?>
<VBox spacing="10" alignment="CENTER" prefWidth="300" prefHeight="300" fx:controller="sample.Controller" xmlns:fx="http://javafx.com/fxml">
<fx:define>
<Font fx:id="FONT" size="18" />
</fx:define>
<StackPane alignment="CENTER">
<Rectangle fill="TRANSPARENT" stroke="GRAY" width="230" height="50" />
<Text fx:id="output" font="$FONT" />
</StackPane>
<HBox spacing="10" alignment="CENTER">
<Button text="7" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="8" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="9" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="/" prefWidth="50" font="$FONT" onAction="#processOperator" />
</HBox>
<HBox spacing="10" alignment="CENTER">
<Button text="4" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="5" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="6" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="*" prefWidth="50" font="$FONT" onAction="#processOperator" />
</HBox>
<HBox spacing="10" alignment="CENTER">
<Button text="1" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="2" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="3" prefWidth="50" font="$FONT" onAction="#processNumpad" />
<Button text="-" prefWidth="50" font="$FONT" onAction="#processOperator" />
</HBox>
<HBox spacing="10" alignment="CENTER">
<Button text="0" prefWidth="110" font="$FONT" onAction="#processNumpad" />
<Button text="=" prefWidth="50" font="$FONT" onAction="#processOperator" />
<Button text="+" prefWidth="50" font="$FONT" onAction="#processOperator" />
</HBox>
</VBox>
Error
> Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
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 sample.Main.start(Main.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
... 1 more
I'm following this tutorial to learn more about JavaFX along with xml.