问题
I've read through all of the issues about this on stackoverflow, and I haven't used something that worked. I have a basic fxml that is built using the JavaFX Scene Builder 1.1 while using Java 1.7.
I just want to load the file... but everything seems to point to a null location meaning that it cant find it. I don't understand why. I have 18 try/catches to show 18 different possibilities for it to work under, but it cant find it. These examples are pulled from some of the stackoverflow questions as 'valid accepted answers'. What am I missing something here? Everything compiles, so I dont think I'm missing a SDK or something major.
The log prints 1 to 18, with a NullPointerException on the 18th try/catch that says NullPointer... location is required.
View post on imgur.com
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
public class OptionsToggleMenu extends Application {
public void launchTheThing(String... args){ //runs this on the Main application's main method.
launch(args);
}
@Override
public void start(Stage primaryStage) {
int failures = 0;
try{
Parent root1 = new Parent() {
};
try{
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(1);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(2);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(3);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(4);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(5);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(6);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(7);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/AdventureLibrary/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(8);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(9);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/bin/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(10);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(11);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(12);
failures++;
}
try{
root1 = FXMLLoader.load(getClass().getResource("/resources/test.fxml"));
System.out.println(null == root1);
}
catch(Exception e){
System.out.println(13);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(14);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(15);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("resources/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(16);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getResource("test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(17);
failures++;
}
try{
Parent root2 = FXMLLoader.load(getClass().getClassLoader().getResource("/test.fxml"));
System.out.println(null == root2);
}
catch(Exception e){
System.out.println(18);
System.out.println(e);
failures++;
}
System.out.println("There are this many failures: " + failures+"/18");
Scene scene = new Scene(root1, 300, 275);
primaryStage.setTitle("FXML Welcome");
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e)
{
System.out.println("XX"+e);
}
}
}
Edit: Adding code that calls on resources/fxml/test.fxml is unsuccessful as well.
回答1:
To fully solve this issue, I took James_D's post and checked how IDEA's build to production works. It doesn't save files unless you tell it to. There are several default files that the compiler will bring to production like .property and .jpg files... but not JavaFX files like that end in .fxml.
I followed this: https://www.jetbrains.com/idea/help/resource-files.html I was able to get a build to deploy my fxml file once I appened ?*.fxml at the end of the compile line for accepted files.
Thanks guys.
回答2:
I think you need to change this line:
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));
I do not think that is the correct path to your FXML. In my project we use
resources
->fxml
text.fxml
We load it like this:
FXMLLoader loader = new FXMLLoader();
loader.load(Class.class.getClassLoader().getResource("fxml/text.fxml").openStream());
来源:https://stackoverflow.com/questions/35630415/nullpointer-location-on-javafx-fxmlloader