3
votes

I can't run my javafx - kotlin app.

My Starter class

class Starter : Application() {

  override fun start(primaryStage: Stage?) {
      val root : Parent = FXMLLoader.load(javaClass.getResource("view/main.fxml"))
      primaryStage?.title = "Title"
      primaryStage?.scene = Scene(root)
      primaryStage?.show()
  }

  fun main(args: Array<String>) {
      launch(args)
  }
}

I can't pass the param "args" to "launch" method because compiler says:

Error:(19, 9) Kotlin: None of the following functions can be called with the arguments supplied: public open fun launch(p0: Class!, vararg p1: String!): Unit defined in javafx.application.Application public open fun launch(vararg p0: String!): Unit defined in javafx.application.Application

If I trying call "launch" method without params I have following Exception

Exception in thread "main" java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767) Caused by: java.lang.NullPointerException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389) at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328) ... 5 more

1
primaryStage will never be null, remove the ? from the type and the variable method calls. - Renato

1 Answers

3
votes

You need to use spread operator

fun main(args: Array<String>) {
    Application.launch(Starter::class.java, *args)
}