javaFX,抛出NullPointerException,位置必填 [英] javaFX, throws NullPointerException, Location is required
问题描述
我看到了其他答案,但没有任何帮助
i have seen other answers but nothing have helped me
(对不起,GUI的新知识只知道swing的基础知识)
(sorry new to GUI only know basics of swing)
这是主要班级
package application;
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) {
try {
Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("/Main.fxml"));
Scene scene = new Scene(root,400,400);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setScene(scene);
primaryStage.show();
}
catch(Exception e) {
e.printStackTrace();
System.exit(0);
}
}
public static void main(String[] args) {
launch(args);
}
}
这是我要调用其方法的另一个类
this is another class whose method i want to invoke
package application;
import java.util.Random;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Label;
public class MainController {
@FXML
private Label myMessage;
void generaterandom (ActionEvent event){
Random rand=new Random();
int myrand=rand.nextInt(50)+1;
myMessage.setText(Integer.toString(myrand));
System.out.println(Integer.toString(myrand));
}
}
如果需要,我将添加xml文件.
i will add xml file if needed .
这是我遇到的错误
java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at application.Main.start(Main.java:14)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl .java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:32 6)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
推荐答案
getClass().getClassLoader().getResource(...)
将从相对于类路径的路径加载资源.由于您已将FXML文件放置在application
pacakge中,因此需要:
getClass().getClassLoader().getResource(...)
will load a resource from a path relative to the classpath. Since you placed the FXML file in the application
pacakge, you need:
Parent root=FXMLLoader.load(getClass().getClassLoader().getResource("application/Main.fxml"));
如果仅使用getClass().getResource(...)
,并且未在路径前添加/
,它将从相对于当前类的路径加载.所以
If you just use getClass().getResource(...)
, and do not prefix the path with /
, it will load from a path relative to the current class. So
Parent root=FXMLLoader.load(getClass().getResource("Main.fxml"));
也应该起作用.
确保将FXML文件和.class
文件一起导出到构建文件夹.
Make sure that your FXML file is being exported to the build folder, along with the .class
files.
这篇关于javaFX,抛出NullPointerException,位置必填的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!