JavaFX and maven: NullPointerException: Location is required
Make sure that your sample.fxml
is in the src/main/resources/
directory (or a subdirectory). Then you can access the file like this:
Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));
Explanation:
During the compile phase all resources and classes get copied to target/classes/
. So your fxml file resides in this directory and your class in a subdirectory regarding its package name. If you call getClass().getResource("sample.fxml");
the file will be searched relative to the class file which will be this directory: target/classes/sample/
.
Calling .getResource()
on the classloader sets the relative search path to target/classes/
and therefore your file gets found.
P.S. You could also write:
Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
JavaFX Location is required. even though it is in the same package
Moving the file to the main/resources directory worked.
java.lang.NullPointerException: Location is required
Two problems exist:
1)The File path
2)The Controller was not set properly
Recommendation:
If you are using SceneBuilder, when you want to see what your controller might look like, you can go to View -> Show Sample Controller Skeleton
.
Solution
Main class:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
System.out.println(getClass().getResource("anwendung.fxml").getPath());
//other solution
//FXMLLoader loader = new FXMLLoader(getClass().getResource("anwendung.fxml"));
//Parent root = loader.load();
//Keep in mind that you are calling a static method
Parent root = FXMLLoader.load(getClass().getResource("anwendung.fxml"));
primaryStage.setTitle("Benutzerverwaltung");
root.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(new Scene(root));
primaryStage.show();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
Controller:
package application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.input.MouseEvent;
public class AnwendungsController {
@FXML
private Button closeButton;
@FXML
void handleCloseButtonAction(ActionEvent event) {
}
@FXML
void onMouseClickedCancelBtn(MouseEvent event) {
}
}
FXML:
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.text.Font?>
<AnchorPane prefHeight="140.0" prefWidth="350.0" xmlns="http://javafx.com/javafx/8.0.60" xmlns:fx="http://javafx.com/fxml/1" fx:controller="application.AnwendungsController">
<children>
<Button fx:id="closeButton" layoutX="18.0" layoutY="100.0" mnemonicParsing="false" onAction="#handleCloseButtonAction" onMouseClicked="#onMouseClickedCancelBtn" prefHeight="26.0" prefWidth="77.0" text="Abbrechen" />
<Label layoutX="10.0" layoutY="27.0" prefHeight="27.0" prefWidth="342.0" text="Sie können das Systema nun verwenden" textAlignment="CENTER" textOverrun="CLIP">
<font>
<Font name="System Bold" size="18.0" />
</font>
</Label>
</children>
</AnchorPane>
Location is required exception when loading FXML file
The short answer is getClass().getResource("sample.fxml")
returns null
silently if the resource cannot be found on the runtime classpath, not the current directory etc.
So this depends on your IDE project setup, if you're using eclipse try adding the folder that sample.fxml
resides in the run configuration.
Some ideas...
- try
getClass().getResource("/sample.fxml")
instead... - try moving
sample.fxml
into the resources folder. I don't know much about your IDE, but I suspect that folder is only used for.java
files... this is certainly true for gradle projects in eclipse - resources have to be in thesrc/main/resources
tree as only that is added to the runtime classpath...
Related Topics
How to Read Text from Hidden Element with Selenium Webdriver
What's the Purpose of Meta-Inf
Should You Call Releasestringutfchars If Getstringutfchars Returned a Copy
Guessing the Encoding of Text Represented as Byte[] in Java
Remove Last Character of a Stringbuilder
How to Make Hashmap Work with Arrays as Key
How to Handle Deserializing with Polymorphism
How to Get Java to Ignore the "Trust Store" and Just Accept Whatever Ssl Certificate It Gets
Why Does Java Switch on Contiguous Ints Appear to Run Faster with Added Cases
Eclipse Memory Settings When Getting "Java Heap Space" and "Out of Memory"
Parsing CSV Input with a Regex in Java
How to Capture Video Using Jmf, But Without Installing Jmf
Java Benchmarking - Why Is the Second Loop Faster
How to Convert .Pfx File to Keystore with Private Key
What's an Example of Duck Typing in Java