JavaFX应用程序在启动时抛出NullPointerException

时间:2015-04-27 10:47:23

标签: java eclipse nullpointerexception javafx-8

更新:即使我删除了对FXMLLoader类的调用,也会出现同样的问题。即使使用空start()方法,也会抛出异常。该问题与目录树结构完全无关。

对于我开始的每个JavaFX项目,我总是遇到NullPointerException在启动时被抛出的问题。即使使用甚至显示舞台所需的最基本代码,也会发生这种情况。我可以使用以下代码复制该问题:

Main.java

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) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("Main.fxml"));
        Scene scene = new Scene(root, 400, 400);
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

Main.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.layout.AnchorPane?>

<AnchorPane xmlns:fx="http://javafx.com/fxml/1">

</AnchorPane>

启动时的堆栈跟踪:

Thread [main] (Suspended (exception NullPointerException))  
SystemProperties.setVersions() line: not available [local variables unavailable]    
SystemProperties.lambda$static$28() line: not available 
314337396.run() line: not available 
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]  
SystemProperties.<clinit>() line: not available 
LauncherImpl.startToolkit() line: not available 
LauncherImpl.launchApplicationWithArgs(String, String, String[]) line: not available    
LauncherImpl.launchApplication(String, String, String[]) line: not available    
NativeMethodAccessorImpl.invoke0(Method, Object, Object[]) line: not available [native method]  
NativeMethodAccessorImpl.invoke(Object, Object[]) line: not available   
DelegatingMethodAccessorImpl.invoke(Object, Object[]) line: not available   
Method.invoke(Object, Object...) line: not available    
LauncherHelper$FXHelper.main(String...) line: not available 

如果我在那之后恢复执行:

Thread [JavaFX Application Thread] (Suspended (exception NullPointerException)) 
PropertyHelper.lambda$getBooleanProperty$510(String) line: not available    
712544800.run() line: not available 
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]  
PropertyHelper.getBooleanProperty(String) line: not available   
Parent.<clinit>() line: not available   
NativeConstructorAccessorImpl.newInstance0(Constructor<?>, Object[]) line: not available [native method]    
NativeConstructorAccessorImpl.newInstance(Object[]) line: not available 
DelegatingConstructorAccessorImpl.newInstance(Object[]) line: not available 
Constructor<T>.newInstance(Object...) line: not available   
Class<T>.newInstance() line: not available  
ReflectUtil.newInstance(Class<?>) line: not available   
FXMLLoader$InstanceDeclarationElement.constructValue() line: not available  
FXMLLoader$InstanceDeclarationElement(FXMLLoader$ValueElement).processStartElement() line: not available    
FXMLLoader.processStartElement() line: not available    
FXMLLoader.loadImpl(InputStream, Class<?>) line: not available  
FXMLLoader.loadImpl(Class<?>) line: not available   
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Callback<Class<?>,Object>, Charset, Class<?>) line: not available  
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Callback<Class<?>,Object>, Class<?>) line: not available   
FXMLLoader.loadImpl(URL, ResourceBundle, BuilderFactory, Class<?>) line: not available  
FXMLLoader.loadImpl(URL, ResourceBundle, Class<?>) line: not available  
FXMLLoader.loadImpl(URL, Class<?>) line: not available  
FXMLLoader.load(URL) line: not available    
Main.start(Stage) line: 10  
LauncherImpl.lambda$launchApplication1$159(AtomicBoolean, Application) line: not available  
1666080238.run() line: not available    
PlatformImpl.lambda$runAndWait$172(Runnable, CountDownLatch) line: not available    
972765878.run() line: not available 
PlatformImpl.lambda$null$170(Runnable) line: not available  
1842446646.run() line: not available    
AccessController.doPrivileged(PrivilegedAction<T>, AccessControlContext) line: not available [native method]    
PlatformImpl.lambda$runLater$171(Runnable, AccessControlContext) line: not available    
1651945012.run() line: not available    
InvokeLaterDispatcher$Future.run() line: not available  
WinApplication._runLoop(Runnable) line: not available [native method]   
WinApplication.lambda$null$145(Runnable) line: not available    
2091156596.run() line: not available    
Thread.run() line: not available    

问题是,如果我再次继续执行,程序就会正常启动。我的阶段显示正确,所有资源都加载没有问题等等。我真的无法想象如果一切按预期工作,JavaFX应该毫不犹豫地抛出NullPointerException。我缺少什么?

我正在使用在Windows 8.1上运行的Eclipse Luna(4.4.2) 这些项目是使用JavaSE-1.8和JavaFX SDK构建的(我认为它是JavaFX 8)

0 个答案:

没有答案