Using the @FXML annotations in Kotlin to get at FXML

Well, a picture is worth a thousand words, so here goes:

But to sumarize, there is first the question of how I should use @FXML at all. Finally I try to sidestep this by going the (possibly questionable) route of declaring the member to be public, but then it seems that kotlin doesn’t realize that JavaFX magic (read: I don’t know exactly how it works) is happening and takes care of the initialization.

Actually, there may be a more fundamental problem. I have fx:controller=“ui.mainController” in my root FXML element. According to intellij, this seems to look good, since it isn’t red, and if I make a typo in “ui.mainController” it will change to red. However, attempting to build with this fx:controller set yields the following stracktrace (now for an actual thousand words…):

FAILURE: Build failed with an exception.

  • What went wrong:
    Execution failed for task ‘:compileKotlin’.
    > Compilation error. See log for more details

  • Try:
    Run with --info or --debug option to get more log output.

  • Exception is:
    org.gradle.api.tasks.TaskExecutionException: Execution failed for task ‘:compileKotlin’.
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(
    at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.PostExecutionAnalysisTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.SkipUpToDateTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.ValidatingTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.SkipEmptySourceFilesTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.SkipTaskWithNoActionsExecuter.execute(
    at org.gradle.api.internal.tasks.execution.SkipOnlyIfTaskExecuter.execute(
    at org.gradle.api.internal.tasks.execution.ExecuteAtMostOnceTaskExecuter.execute(
    at org.gradle.api.internal.AbstractTask.executeWithoutThrowingTaskFailure(
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.executeTask(
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$TaskExecutorWorker.processTask(
    at org.gradle.execution.taskgraph.AbstractTaskPlanExecutor$
    at org.gradle.execution.taskgraph.DefaultTaskPlanExecutor.process(
    at org.gradle.execution.taskgraph.DefaultTaskGraphExecuter.execute(
    at org.gradle.execution.SelectedTaskExecutionAction.execute(
    at org.gradle.execution.DefaultBuildExecuter.execute(
    at org.gradle.execution.DefaultBuildExecuter.access$200(
    at org.gradle.execution.DefaultBuildExecuter$2.proceed(
    at org.gradle.execution.DryRunBuildExecutionAction.execute(
    at org.gradle.execution.DefaultBuildExecuter.execute(
    at org.gradle.execution.DefaultBuildExecuter.execute(
    at org.gradle.initialization.DefaultGradleLauncher.doBuildStages(
    at org.gradle.initialization.DefaultGradleLauncher.doBuild(
    at org.gradle.launcher.exec.InProcessBuildActionExecuter$
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(
    at org.gradle.launcher.exec.InProcessBuildActionExecuter.execute(
    at org.gradle.internal.Actions$RunnableActionAdapter.execute(
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(
    at org.gradle.launcher.cli.CommandLineActionFactory$ParseAndBuildAction.execute(
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(
    at org.gradle.launcher.cli.JavaRuntimeValidationAction.execute(
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
    at org.gradle.launcher.cli.CommandLineActionFactory$WithLogging.execute(
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(
    at org.gradle.launcher.cli.ExceptionReportingAction.execute(
    at org.gradle.launcher.Main.doAction(
    at org.gradle.launcher.bootstrap.ProcessBootstrap.runNoExit(
    at org.gradle.launcher.GradleMain.main(
    at com.intellij.rt.execution.application.AppMain.main(
    Caused by: org.gradle.api.GradleException: Compilation error. See log for more details
    at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.callCompiler(Tasks.kt:119)
    at org.jetbrains.kotlin.gradle.tasks.AbstractKotlinCompile.compile(Tasks.kt:95)
    at org.gradle.internal.reflect.JavaMethod.invoke(
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.doExecute(
    at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(

at org.gradle.api.internal.project.taskfactory.AnnotationProcessingTaskFactory$StandardTaskAction.execute(
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(
at org.gradle.api.internal.AbstractTask$TaskActionWrapper.execute(
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeAction(
at org.gradle.api.internal.tasks.execution.ExecuteActionsTaskExecuter.executeActions(
… 45 more

Here is how mainController.kt looks:

package ui;

import javafx.stage.Stage
import javafx.fxml.FXMLLoader
import javafx.scene.control.TabPane
import javafx.fxml.FXML
import javafx.scene.control.TableView

class mainController : TabPane() {   val root: TabPane   public val logTable: TableView

  try {
           root = FXMLLoader.load(
  } catch (e: IOException) { throw RuntimeException() }


Hi Brandon,

I can’t say I’ve played with JavaFX, but here’s a few general tips that might help:

  • "@FXML" is not an annotation in Kotlin, use unadorned “FXML”. ("@" is used for labels. In a few rare cases you need to put the annotation in square brackets)
  • You probably need vars and not vals and they must be initialised. You have two options:
      FXML var logTable: TableView<>? = null
      FXML var logTable: TableView<
      > by Delegates.notNull()

The first one just assigns it to null, but can be painfull if in reality it’s going to be not null when initialised.

The second one uses delegates to treat it as not null. This will throw a NPE on use if not initialised by JavaFX.

Also note you need to specify a type parameter for TableView as there are no raw types in Kotlin - I’ve used a projection (*), but replace it with a real value where sensible.

How well all of this works with JavaFX and with what you’re trying to do, I can’t really say.

Good luck!


Thanks Andrew,

This looks like a step in the right direction and also clears up a number of my misconceptions.

I think the more fundamental issue arises when I specify the Kotlin controller class (as mentioned above) within the FXML root element:

I’d like to look into this more, but frankly don’t know where to start. It appears that there occurs on line 26 with the FXML load. This will work fine if I don’t specify the controller class. Additionally, from the attached stracktrace, it looks as though JavaFX is having difficulty constructing an application that includes a kotlin controller class, but I can’t make much more sense of it than that.

[|attachment](upload:// (1.57 KB)

The gradle compiler log doesn't tell us much, I'm afraid. You can use Kotlin with JavaFX though. Some of us are doing it.

Make sure your class doesn’t have any constructor parameters. FXML wants to construct the controller class for you, which means, you must have a no-args constructor.

Good to know! But I don't believe my constructor class is requiring any parameters, as you mentioned. You can see it at the bottom of my first post, but since I'm not sure of how to best show useful output (please let me know if I miss something obvious), I've included the entire project as a zip file.

[|attachment](upload:// (1.03 MB)

Hi, As I understand JavaFx, you have a recursion in loading mainController class because you load main.fmxl inside it and in main.fxml you have a reference to mainController class. To fix a recuirsion you should load mainController from main.fxml once outside of class itself. ex.

val root: TabPane = try {   FXMLLoader.load(javaClass<mainController>().getClassLoader().getResource("main.fxml")) } catch (e: IOException) { throw RuntimeException() }

class mainController : TabPane() {
  FXML var logTable: TableView<*> by Delegates.notNull()

That was a very insightful observation, though I'm also a bit ashamed for not noticing at least the possibility.  I am now pretty sure it arose because I was reading two separate JavaFX examples on two different evenings and muddling them together in my mind. Many thanks to all on this error.

A few followup issues. After taking some time to upgrade to M11 and mess around with getting kotlinfx and M11, I went back to this project and had some issues actually using logTabe. Using this:

class mainController : TabPane() {

  FXML var logTable: TableView<logEntry> by Delegates.notNull();
  // FXML var logTable: TableView<logEntry>? = null;
  init {
  // Some simple Test Code
  var x = logEntry()
  val xs: ArrayList<logEntry> = ArrayList()
  var initList: ObservableList<logEntry> = FXCollections.observableArrayList(xs);

  logTable.setItems(initList) // line 36 or 37 in errors below


The first declaration of logTable (uncommented above) results in the following error:

$ ~/gradle-2.3/bin/gradle ui
:compileKotlin UP-TO-DATE
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:classes UP-TO-DATE
:uiException in Application constructor
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: Unable to construct Application instance: class ui.LaRGUI
  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.reflect.InvocationTargetException
  at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
  at sun.reflect.NativeConstructorAccessorImpl.newInstance(
  at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(
  at java.lang.reflect.Constructor.newInstance(
  at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$152(
  at com.sun.javafx.application.LauncherImpl$$Lambda$49/ 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
Caused by: java.lang.IllegalStateException: Property logTable should be initialized before get
  at ui.mainController.getLogTable(mainController.kt)
  at ui.mainController.<init>(mainController.kt:37)
  at ui.LaRGUI.<init>(main.kt:23)
  … 18 more

By the way, since I updated the IntelliJ plugin to M11, it seems to have trouble finding Delegates in “import” (all occurrences of Delegates appears in red is noted as being an unresolved reference).

Commenting the first declaration of logTable out and going with the null asignment results in this:

$ ~/gradle-2.3/bin/gradle ui :compileKotline: C:cygwin64homebrand_000LaRsrcmainkotlinmainController.kt: (36, 17): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type javafx.scene.control.TableView<datalog.logEntry>? FAILED

A quick google search showed I should be doing this, which fixes this problem (but not if I revert to the Delegates.notNull() method):


So correct me if I’m wrong in desiring this, but is there a way to assert that logTable is not null, so that the first method will work, or so that I don’t need to use the safe call operator every time for something that must be safe (if we can’t get ahold of a fundamental UI element, we have big problems). Furthermore, maybe this is a problem with how Kotlin Delegates is working in general (at least in M11)?

Arg, I think at least part of the Delegates problem is that my project/lib directory still had old files for:

kotlin-runtime.jar          kotlin-runtime-sources.jar

How do you update these? Either from IntelliJ or manually?

Edit: I found only the kotlin-runtime.jar in the plugin bundle, but this wasn’t enough to fix the undefined references issue in IntelliJ.

If you delete the jars and run Configure Kotlin in Project action in the IDE, new jars will be put in (in fact the IDE should have suggested updating them automatically) It will also probably add kotlin-reflect.jar

One thing I don’t quite get here is why you have some jars in the lib/ directory when using Gradle in your build process

Aha, this clears up several things.

Firstly, I’d been getting a message to configure kotlin, but each time I did this it seemed to mess up my gradle file (through no fault of the plugin - it is just that I already had a gradle file that was working for kotlin).
But it was trivial to fix the changes, which amounted to removing some of my older (redundant) gradle statements, and using ‘kotlin_version’ instead of ‘kotlinVersion’.

I deleted the lib directory … you are right, totally unnecessary.

Eventually I had to delete my entire .idea directory; things seemed to be in a bad state somehow (not only Kotlin-related things; ‘javaHome’ was not being detected, despite my specification not to use JAVA_HOME for gradle in IntelliJ). Now everything is going smoothly. See here for more info:

Well, I should note that the following issue still occurs if trying to use:

FXML var logTable: TableView<logEntry> by Delegates.notNull();

Caused by: java.lang.IllegalStateException: Property logTable should be initialized before get
at ui.mainController.getLogTable(mainController.kt)
at ui.mainController.<init>(mainController.kt:37)
at ui.LaRGUI.<init>(main.kt:23)
… 18 more

But perhaps it is time for a new discussion for the sake of brevity.


I think that using Delegates.notNull() will not work without doing some additional “magic”.

Lets says that we have this declaration

FXML var tabPane: TabPane by Delegates.notNull()

At Kotlin Bytecode we can see that tabPane is converted into field_name$delegate and it's type is migrated to ReadWriteProperty. To set TabPane it is required to invoke setTabPane method which FXMLLoader will not do.

I think that in order to make FXML annotaton works we(community) have to create custom FXMLLoader and maybe some custom Delegator (witch allow make FXML fields as values not variables).

In my opinion someting like this should be a nice way to bind FXML components within controller class:

val tabPane: TabPane by bindFXML()

Where bindFXML() should be initialized by custom FXMLLoader

I will try do some proof-of-concept in few next days.

Something else that might be quite nice is an FXML to Kotlin compiler. There's one for FXML to Java already. Parsing FXML is quite slow due to all the reflection, which the JVM doesn't optimise very well. Most of the time of course it's irrelevant but for performance sensitive apps it can be an issue.

By the way, FXMLLoader has a BeanAdapter system. I think, subclassing BeanAdapter and using the new Kotlin reflection tools might work better than making a custom FXMLLoader.


I have been thinking about some requirements and features that would be nice to have while working with Kotlin and FXML files.

Must have:

  1. Injected fields should be val’s not var’s.
  2. Simple and clear syntax.
  3. Runtime validation of declared fields.

Good to have:

  1. Do not copy and modify code from original FXMLLoader.
  2. Do not use private API.
  3. No need to inherit other class or implement trait.
  4. Compile time validation of declared fields.

Today I was experimenting with FXMLLoader class (and some of other classes that is uses) and I was able to create proof-of-concept app whic. It is avaliable at

I think it fulfils all must haves, but it has some problems with good to haves. Unfortunately FXMLLoader does not have any callback mechanism. I was able to plug into private methods which will not be a part of JavaFX standard.

The API is really simple. It consists two classes: bindFXML and bindOptionalFXML. First one inform that field is require and if it is not present IllegalStateException will be raised. The other one will be injected when proper Object is available, but will not raise exception when there is no such object. e.g.

val button : Button by bindFXML()
val optionalButton: Button by bindOptionalFXML()

There are other ways to consider. For example it is possible to inject all fields using only public API, but it would require Controller class to inherit from some AbstractController class which would store all values in map and during initialization inject all fields. It would be much less elegant solution, but it would always work - despite changes in internal API.

As Mike wrote it is also possible to create FXML to Kotlin compiler. I am not sure what is the best way to do it, but it probably require to rely on some private API or require rewriting FXMLLoader (usage code of existing FXML to Java should be investigated first). But it would make Controller syntax more verbose. Because it either create template class (with is really ugly solution) or either create abstract class which some abstract methods (but how to set generic type for e.g. ListView).

Hello, Paweł

I review your code and think that you at the right direction. Your solution with bindFXML() looks pretty good.

I try to use your code on my project and retrieve several error messages. To fix it I firstly change default value of builder to allow use Font and PropertyValueFactory classes into .fxml file (they was no be initialized correctly). I found this solution in FXMLLoader class sources.

Secondly, I increase your tag deep counter in each callback with “begin…Element” name (It allow me use <children> tag in .fxml).

And at the last I replace your code which work only with Node class instances to my code which require getId method presence (it allow me bind TableColumn class instances).


I change type of KotlinFXMLLoader from object to class to enable class calling from java code. To do it I inherit KotlinFXMLLoader from FXMLLoader and reload two method (load() and load(inputStream: InputStream?)). Now load(inputStream: InputStream?) method has no implementation. But I did not now how to override static load() methods in kotlin code to hide his implementation.

Currently I make a pull request to your repo. Hope It would be helpful.

Here is a working example of using @FXML with lateinit var:

class SampleFXML  {

    @FXML lateinit var actionTarget: Text

    fun start(primaryStage: Stage) {
        val root = FXMLLoader.load<Parent>("sample/sample.fxml"))
        val scene = Scene(root, 300.0, 275.0)
        primaryStage.title = "FXML Welcome"
        primaryStage.scene = scene

    fun handleSubmitButtonAction(event: ActionEvent) {
        actionTarget.text = "Sign in button pressed"


<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.text.*?>

<GridPane fx:controller="sample.SampleFXML"
          xmlns:fx="" alignment="center" hgap="10" vgap="10">

    <Button text="Sign In"

    <Text fx:id="actionTarget"
          GridPane.columnIndex="1" GridPane.rowIndex="6"/>