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.


Do we have any better solutions to this?
I am currently using the below, which does not work sometimes,

private val soundUri = Uri.parse("android.resource://" +`package`!!.name + "/" + R.raw.audio_file)


This is not a Kotlin problem, rather an android problem. You’re supposed to access resources (based on a context) using the Resources class. In this case or might be the solution you’re looking for. Note that neither is accessible from outside the package. Btw. You get the package not from the MainActivity, but you can inject it into BuildConfig through a gradle configuration


The two methods you have linked in the response, return InputStream, I would require a Uri to the resource file.

Thank you for your reply.


You can do that. There is some further info here. But what is called package has been renamed to applicationId. It is the name of your app as far as android is concerned (the name in the manifest). It is actually available from the Context

This answer for images has a good solution


Actually, my solution for Uri works. I just wanted to have a cleaner way of writing the Uri to a resource file.

Only!!.name portion of the string is a bit weird. I wanted this to be a bit cleaner.

Edit: and above mentioned version of string does not work when I enable the debug version (using applicationIdSuffix “.debug”) of the app to be deployed on the device. Some light on the debug thing here.

Thank you for the links. :slight_smile: