gpt4 book ai didi

java - JavaFX FXMLLoader 上的 NullPointer 位置

转载 作者:行者123 更新时间:2023-12-01 10:20:57 26 4
gpt4 key购买 nike

我已经阅读了 stackoverflow 上有关此问题的所有问题,但我还没有使用过有效的方法。我有一个基本的 fxml,它是使用 JavaFX Scene Builder 1.1 和 Java 1.7 构建的。

我只想加载文件...但所有内容似乎都指向空位置,这意味着它找不到它。我不明白为什么。我有 18 个 try/catch 来展示它工作的 18 种不同可能性,但它找不到它。这些示例取自一些 stackoverflow 问题,作为“有效接受的答案”。我在这里缺少什么?一切都会编译,所以我不认为我缺少 SDK 或主要的东西。

日志打印 1 到 18,并在第 18 次 try/catch 上出现 NullPointerException,表示需要 NullPointer... 位置。

View post on imgur.com

enter image description here

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);
}
}


}

编辑:添加调用 resources/fxml/test.fxml 的代码也不成功。 enter image description here

最佳答案

为了彻底解决这个问题,我引用了 James_D 的帖子并检查了 IDEA 的构建到生产的工作原理。除非您告诉它,否则它不会保存文件。编译器会将几个默认文件带入生产环境,例如 .property 和 .jpg 文件...但不会是像以 .fxml 结尾的 JavaFX 文件。

我关注的是:https://www.jetbrains.com/idea/help/resource-files.html在接受文件的编译行末尾附加 ?*.fxml 后,我就能够获得一个构建来部署我的 fxml 文件。

谢谢大家。

关于java - JavaFX FXMLLoader 上的 NullPointer 位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35630415/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com