JavaFX application throws NullPointerException at

2019-07-21 21:13发布

问题:

UPDATE: The same problem occurs even if I remove the call to the FXMLLoader class. The exception is thrown even with an empty start() method. The problem isn't related at all to the directory tree structure.

For every JavaFX project I start, I always have the issue of NullPointerExceptions being thrown at startup. This happens even with the most basic code needed to even show a stage. I can replicate the issue with the following code:

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>

Stack trace from startup:

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 

And if I resume execution after that:

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    

The thing is, if I continue execution again, the program starts just fine. My stages are displayed properly, all resources are loaded without issue and so on. I really can't imagine that JavaFX is supposed to throw NullPointerExceptions all willy-nilly if everything works as intended. Anything I am missing?

I am using Eclipse Luna (4.4.2) running on Windows 8.1 The projects are built using JavaSE-1.8 and JavaFX SDK (I think it's JavaFX 8)