简体   繁体   中英

Can't run JavaFX - Kotlin app

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

You need to use spread operator

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM