此代码有问题吗?我看到这个信息引起:java.lang.NullPointerException 在pl.moja.biblioteczka.controllers.MainController.setCenter(MainController.java:34) 我不知道为什么。我检查了整个代码,但我没有看到任何拼写错误。
FXMLLoader loader = new FXMLLoader(this.getClass().getResource(fxmlPath));
ResourceBundle bundle = ResourceBundle.getBundle("bundles.messages");
loader.setResources(bundle);
Parent parent = null;
try {
parent = loader.load();
} catch (IOException e) {
e.printStackTrace();
}
borderPane.setCenter(parent);
}
答案 0 :(得分:0)
检查borderPane实例。它显然是空的。
答案 1 :(得分:0)
可能是因为borderPane或parent为null。
borderPane - 我们无法在代码中看到它是如何初始化的,所以它也需要检查
parent - 它的初始值为null,并且您正试图通过loader.load()为其赋值。但是,如果此调用遇到异常,则父级将保持为null(因此为NPException)。或者,如果没有异常(您正在记录堆栈跟踪,以便您知道是否存在),那么loader.load()本身可能会返回null
希望有所帮助