I'm using the latest build on the build server and have a very simple problem I can't seem to figure out.
I have a main function:
fun main(args: Array<String>) { Application.launch(*args)}
I’m calling the launch method of the JavaFX Application class which seem to be expecting a varargs String?. The IDE gives me an error that no function can be found. I’ve attached a screenshot of the code and error.
I can’t change the args parameter to be Array<String?> because it can’t be executed as a program.
So this is disallowed by Kotlin ... and for good reason. The problem is that Array is mutable ... consider the following example:
fun foo(stuff: Array<String?>) {
stuff[0] = null; // perfectly legal, since stuff is : Array<String?>
}
fun bar() {
val args: Array<String> = array(“First”, “Second”, “Third”)
foo(args) // this isn’t legal but we’ll pretend it is …
val thing: String = args[0]
println(thing.length) // What Null pointer exception!! but thing isn’t nullable!!
}
Best way to solve this in your case is probably just to make a copy …
fun main(args: Array<String>) { val copy = Array<String?>(args.size, { i -> args[i] }) Application.launch(*copy)}