2

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)

sara
  • 3,521
  • 14
  • 34
  • Verify so that your Main.fxml file is in src/main/resources/ folder – crisu Apr 27 '15 at 10:58
  • Why? `FXMLLoader` doesn't enforce a certain directory tree, does it? Also, this did not solve the issue, or indeed make any change whatsoever. – sara Apr 27 '15 at 13:01
  • The file is searched relative to the class file, the code should be Parent root = FXMLLoader.load(getClass().getResource("/Main.fxml")); – crisu Apr 27 '15 at 14:33
  • This also makes no difference at all. My problem isn't that the fxml file isn't found, the program starts alright, only the exception gets thrown first. Also, see the update – sara Apr 27 '15 at 14:45
  • I'm going to analyze it and see if I can figure out why this is happening, your error it's really interesting. – crisu Apr 27 '15 at 15:05
  • @kai Hi, is it possible for you to check (decompile perhaps) the `SystemProperties` class and then post the code for the function `setVersions`? – kucing_terbang Apr 27 '15 at 15:25
  • @kucing_terbang I'm not sure how to decompile java bytecode, but I did find the bytecode for the method at least. Let me know if it helps or if I can do something more. http://pastebin.com/qcuQQRJB – sara Apr 27 '15 at 15:35
  • @kai I'm afraid it's still not clear to see what actually happened on your place. I have looked up the code and the reason I can think of is because the runtime (javafx runtime to be precise) is trying to load `version.properties` file which could be not exists (but in my place, the code indicate that if there is an error, then just ignore it) and the second error is shown when it trying to load property called `javafx.sg.warn`. So, to make sure what happened, it would be great if you can just either get the source or decompile it. Or maybe you could tell me what `java -version` result? – kucing_terbang Apr 28 '15 at 03:31
  • java version "1.8.0_45" Java(TM) SE Runtime Environment (build 1.8.0_45-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode) as for the source code, I have no idea how one goes about decompiling byte code. – sara Apr 29 '15 at 22:34
  • Did you ever find a solution to this? I'm also encountering this, everything works fine though just the nullpointerexception being thrown and caught by the javafx library. – Emily L. Nov 06 '15 at 21:32
  • I'm afraid not, I finished the assignment (it was for school) and handed it in and passed, haven't looked at JavaFX since. Now I'm working in C#.NET and javascript. – sara Nov 07 '15 at 01:14
  • I answered this question on another question. I have seen just now, that this one was first. The answer can be found [here](https://stackoverflow.com/a/54061934/4306369) – Thorben Kuck Jan 07 '19 at 11:30

0 Answers0