我已经阅读了关于堆栈溢出的所有问题,而且我没有使用一些有用的东西。我有一个基本的fxml,它是使用JavaFX场景生成器1.1生成的,同时使用Java1.7。
我只想载入文件..。但一切似乎指向一个空位置,这意味着它找不到它。我不明白为什么。我有18次尝试/捕捉来展示它在18种不同的可能性下工作,但它找不到。这些例子是从一些堆叠溢出的问题中抽取出来的,它们是“有效的、可接受的答案”。我漏掉了什么东西?所有的东西都会编译,所以我不认为我缺少一个SDK或一些重要的东西。
日志打印1到18,第18次尝试/捕捉时有一个NullPointerException,上面写着NullPointer.需要地点。

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);
}
}
}编辑:添加调用资源/fxml/test.fxml的代码也是不成功的。

发布于 2016-02-25 15:49:32
为了完全解决这个问题,我接受了James_D的帖子,并检查了IDEA的构建是如何工作的。除非你告诉它,否则它不会保存文件。编译器将为生产带来几个默认文件,如.property和.jpg文件.但这样的JavaFX文件在.fxml中没有结束。
我遵循了以下步骤:在编译行的末尾为接受的文件追加了https://www.jetbrains.com/idea/help/resource-files.html之后,我就能够获得一个构建来部署我的fxml文件。
谢谢你们。
发布于 2016-02-25 14:58:08
我认为你需要改变这句话:
root1 = FXMLLoader.load(getClass().getResource("AdventureLibrary/test.fxml"));我不认为这是通往FXML的正确路径。在我的项目中我们使用
resources
->fxml
text.fxml我们像这样装载它:
FXMLLoader loader = new FXMLLoader();
loader.load(Class.class.getClassLoader().getResource("fxml/text.fxml").openStream());https://stackoverflow.com/questions/35630415
复制相似问题