I'm trying to add a Global font to my javaFX application using CSS. I followed this answer, however I can't seem to load the font, and the error message is not helpful.
jan 12, 2022 12:24:25 PM javafx.css.CssParser reportException
WARNING: Please report java.lang.NullPointerException at:
The error messages finishes here, not showing where the problem is.
My code directory: pacman directory
My main.java:
package finalPacman;
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{
FXMLLoader loader = new FXMLLoader(getClass().getResource("pacman.fxml"));
Parent root = loader.load();
primaryStage.setTitle("PacMan");
Controller controller = loader.getController();
root.setOnKeyPressed(controller);
double sceneWidth = controller.getBoardWidth() + 20.0;
double sceneHeight = controller.getBoardHeight() + 100.0;
Scene scene = new Scene(root, sceneWidth, sceneHeight);
scene.getStylesheets().clear();
scene.getStylesheets().add(getClass().getResource("pacman.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
root.requestFocus();
}
public static void main(String[] args) {
launch(args);
}
}
My pacman.css:
@font-face {
font-family: 'Pixeboy';
src: url("/src/res/fonts/Pixeboy.ttf");
}
.root{
-fx-font-size: 16;
-fx-font-family: "Pixeboy";
}