How to find resources in Kotlin?


I thought I'd find the answer to this, and indeed my code compiles, but I eventually get a runtime error.

Here’s some simple code that tries to create a JavaFX UI from an FXML specification (a resource):

package ui

import javafx.application.Application
import javafx.stage.Stage
import javafx.fxml.FXMLLoader

fun main(args: Array<String>) {

class LaRGUI : Application() {
  override fun start(stage: Stage?) {
  Stage(stage, title = “Log and Report”) {
           scene = Scene(width = 900.0, height = 550.0) {
           root = FXMLLoader.load(javaClass.getResource(“main.fxml”));

Here is the error:

$ ~/gradle-2.3/bin/gradle ui
w: Annotations path entry points to a non-existent location:
:compileJava UP-TO-DATE
nullException in Application start method
Exception in thread “main” java.lang.reflect.InvocationTargetException
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(
  at java.lang.reflect.Method.invoke(
  at sun.launcher.LauncherHelper$FXHelper.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
  at com.sun.javafx.application.LauncherImpl.launchApplication1(
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(
  at com.sun.javafx.application.LauncherImpl$$Lambda$48/ Source)
Caused by: java.lang.NullPointerException: Location is required.
  at javafx.fxml.FXMLLoader.loadImpl(
  at javafx.fxml.FXMLLoader.loadImpl(
  at javafx.fxml.FXMLLoader.loadImpl(
  at javafx.fxml.FXMLLoader.loadImpl(
  at javafx.fxml.FXMLLoader.loadImpl(
  at javafx.fxml.FXMLLoader.load(
  at ui.LaRGUI$start$1$1.invoke(main.kt:24)
  at ui.LaRGUI$start$1$1.invoke(main.kt:19)
  at Source)
  at ui.LaRGUI$start$1.invoke(main.kt:23)
  at ui.LaRGUI$start$1.invoke(main.kt:19)
  at Source)
  at ui.LaRGUI.start(main.kt:22)
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(
  at com.sun.javafx.application.LauncherImpl$$Lambda$51/ Source)
  at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(
  at com.sun.javafx.application.PlatformImpl$$Lambda$45/ Source)
  at com.sun.javafx.application.PlatformImpl.lambda$null$164(
  at com.sun.javafx.application.PlatformImpl$$Lambda$47/ Source)
  at Method)
  at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(
  at com.sun.javafx.application.PlatformImpl$$Lambda$46/ Source)
  at Method)
  at$$Lambda$37/ Source)
  … 1 more

I’ve attached an image of my project structure. Also, I verfied main.fxml is getting put into the jar file:

$ unzip LaR.jar
Archive:  LaR.jar
  creating: META-INF/
  creating: ui/
  inflating: ui/LaRGUI$start$1$1.class
  inflating: ui/LaRGUI$start$1.class
  inflating: ui/LaRGUI.class
  inflating: ui/UiPackage$main$95ba707c.class
  inflating: ui/UiPackage.class
  inflating: main.fxml

Any ideas what I may be doing wrong? Thanks!

LaR_Resource.PNG (75.8 KB)




I had actually looked at that page, but didn't see anything very helpful. getClass is apparently replaced by javaClass in Kotlin, which I was able to compile successfuly. I believe I've also put my fxml resource in the designated place.


Have you tried "javaClass.getClassLoader().getResource("main.fxml")" or "javaClass.getResource("/main.fxml")"


Thanks, it was a bit strange to me that getResource existed at two levels; I suppose the way I was using it was trying to get the resource of the javaClass class...

Also, I had to specify the class like this:


I saw some code floading around like this:


Any idea what the double “!” operator is?


Its explained in the documentation:


This trick does not seems to work now from inside lambdas. Any idea how to get the resources? I am using lambda as part of a dsl.

javaClass.classLoader.getResource("") always returns null from lambda, but it is returning the resources from a normal top-level class.