我试图将值添加到我使用SceneBuilder创建的FXML文件中的ChoiceBox,但是当我尝试这样做时(在主类中),我得到一个NullPointerException无论如何我试试。请注意,当我在initialize()
方法中注释掉ChoiceBox代码时,项目成功运行(见下文),因此NPE不是另一个原因。
其他一些观点:
我将ChoiceBox代码移动到start()
方法,但这只是将问题移到了文件中的另一行。你知道原因是什么吗?我将非常感激,因为我已经花了两个小时解决这个问题...
在我的FXML文件中,我有
<ChoiceBox fx:id="itemKeuze" layoutX="132.0" layoutY="33.0" prefWidth="150.0" />
在我的主要课程中,我有这个(修剪过的)代码:
public class HoofdScherm extends Application {
@FXML
private ChoiceBox<String> itemKeuze;
@Override
public void start(Stage scherm) throws IOException
{
// The setup and loading of the FXML file...
}
@FXML
private void initialize()
{
// Now I'm only trying to disable the combobox instead of setting the values but the NPE is also thrown
itemKeuze.setDisable(true);
}
private void toonItemScherm()
{
Parent itemScherm = null;
try {
itemScherm = FXMLLoader.load(getClass().getResource("ItemScherm.fxml"));
} catch (Exception e) {
System.out.println("Kon itemScherm niet laden: " + e.getMessage());
}
//itemKeuze.setItems(FXCollections.observableArrayList("CD", "Film", "Spel"));
//itemKeuze.setDisable(true);
stage = new Stage();
stage.setTitle("Voeg item toe");
stage.setResizable(false);
stage.setAlwaysOnTop(true);
stage.setScene(new Scene(itemScherm));
stage.show();
}
}
堆栈跟踪
Exception in Application start method
Exception in thread "main" java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: javafx.fxml.LoadException:
/C:/Users/Isaak/Documents/workspace/JavaPracticumOpdracht3/bin/view/HoofdScherm.fxml
at javafx.fxml.FXMLLoader.constructLoadException(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at view.HoofdScherm.start(HoofdScherm.java:38)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
... 1 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.Trampoline.invoke(Unknown Source)
at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.reflect.misc.MethodUtil.invoke(Unknown Source)
... 18 more
Caused by: java.lang.NullPointerException
at view.HoofdScherm.initialize(HoofdScherm.java:109)
... 27 more
答案 0 :(得分:0)
我找到了答案,部分归功于James_D的观点。
我没有将控制器添加到第二个FXML文件$(document).ready(function(){
var isActive = true;
$("#hamburger").click(function(){
if (isActive) {
$(".banner").headroom("destroy");
isActive = false;
}
else {
(".banner").headroom("init");
isActive = true;
}
});
});
中的(AnchorPane)。因为没有设置控制器,所以不能注入变量(fx:id - &gt;类变量)。我还为每个FXML文件创建了一个单独的控制器,因为所有内容都在一个扩展Apllication类的主类中。这也可能是问题的一部分。