I'm using JavaFX + FXML together with Scala to build a GUI but I get a NullPointerException in the Controller for all Controls from the FXML-file when I use a for-loop or a mapping (of a Scala Collection).
Info: I programmed the exact same example completely in Java and used a for-loop and it worked. It only fails with Scala.
I constructed a very simple FXML + Controller so you can reproduce the Failure (you need to out-comment one of the loops to test it).
Here's my simple FXML (example.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.scene.layout.GridPane?>
<GridPane fx:id="grid" prefHeight="289.0" prefWidth="473.0" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="application.Controller2">
</GridPane>
and here's the Controller2.scala:
package application
import javafx.fxml.FXML
import javafx.scene.layout.{ GridPane, ColumnConstraints }
import javafx.scene.shape.Rectangle;
class Controller2 {
@FXML private var grid: GridPane = _
@FXML def initialize = {
println(grid)
// doesn't work
for (i <- 0 until 100) {
grid.getColumnConstraints().add(new ColumnConstraints(100));
grid.add(new Rectangle(10, 10), i, 0);
}
// works
var i = 0
while (i < 100) {
grid.getColumnConstraints().add(new ColumnConstraints(100));
grid.add(new Rectangle(10, 10), i, 0);
i = i + 1
}
}
}
Any insight into this matter would be highly appreciated