FXML control always null when using Kotlin
As mentioned before. Check if fx:id is set.
Is also possible to use lateinit
modifier.
Your code could looks like:
import javafx.fxml.FXML
import javafx.scene.control.Label
class MainWindowController {
@FXML
lateinit var helloLabel : Label
}
Just like with Java constructors, fx:id
fields will not be populated before but after the init
(or in Java the constructor) is called. A common solution is to implement the Initializable
interface (or just define an initialize()
method) and do additional setup inside the method like so:
import javafx.fxml.FXML
import javafx.scene.control.Label
class MainWindowController : Initializable {
@FXML
var helloLabel: Label? = null
override fun initialize(location: URL?, resources: ResourceBundle?) {
println("Label is null? ${helloLabel == null}")
}
}