title: Scala - Get FXML UI-Elements
body: <p>I´m working on a Scala/JavaFX project with IntelliJ and a plugin for scala.<br>
  The typical way in Java to access elements of your fxml-file is to set an id for each element you want to access and then in your controllerclass declare a variable like "@FXML private Label mylabelid;".<br>
  In Scala it works almost the same way as you can see in my source code. BUT i get a NullPointerException in line 73 (marked with a PROBLEM-comment). This is the only label, which is just set to null. I tried diffrent things. Every single element is getting set as expected apart from missingInputLabel.<br>
  LoginView.fxml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;?import java.net.*?&gt;
  &lt;?import javafx.scene.control.*?&gt;
  &lt;?import javafx.scene.layout.*?&gt;
  &lt;GridPane xmlns:fx="http://javafx.com/fxml/1" fx:controller="logingui.LoginController"&gt;
      &lt;stylesheets&gt;
          &lt;URL value="@/data/gtevStyle.css" /&gt;
      &lt;/stylesheets&gt;
  
      &lt;Label text="SSH-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="0" /&gt;
      &lt;TextField fx:id="sshNameField" promptText="SSH-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="0" /&gt;
      &lt;Label text="SSH-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="1" /&gt;
      &lt;PasswordField fx:id="sshPasswdField" promptText="SSH-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="1" /&gt;
      &lt;Label text="Datenbank-Nutzername:" GridPane.columnIndex="0" GridPane.rowIndex="2" /&gt;
      &lt;TextField fx:id="databaseNameField" promptText="Datenbank-Nutzername" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="2" /&gt;
      &lt;Label text="Datenbank-Passwort:" GridPane.columnIndex="0" GridPane.rowIndex="3" /&gt;
      &lt;PasswordField fx:id="databasePasswdField" promptText="Datenbank-Passwort" onKeyReleased="#onKeyReleased_textField" GridPane.columnIndex="1" GridPane.rowIndex="3" /&gt;
  
      &lt;Label text="Name oder Passwort falsch" fx:id="nameOrPasswdWrong" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="4" /&gt;
      &lt;Label text="Fehlende Angaben" fx:id="missingInputLabel" styleClass="warningLabel" GridPane.columnIndex="1" GridPane.rowIndex="5" /&gt;
      &lt;Button text="Login" onAction="#login" GridPane.columnIndex="0" GridPane.rowIndex="5" /&gt;
  &lt;/GridPane&gt;
  </code></pre>
  
  <p>LoginController.scala:</p>
  
  <pre><code>package logingui
  
  import java.net.URL
  import java.sql.SQLException
  import java.util.ResourceBundle
  import java.util.logging.Level
  import java.util.logging.Logger
  import javafx.fxml.FXML
  import javafx.fxml.Initializable
  import javafx.scene.control.Label
  import javafx.scene.control.PasswordField
  import javafx.scene.control.TextField
  import javafx.scene.input.KeyEvent
  import javafx.scene.input.KeyCode
  import connection.GTEVConnection
  import javafx.stage.Stage
  
  class LoginController extends Initializable {
    @FXML
    private var nameOrPasswdWrong: Label = _
    @FXML
    private var missingInputLabel: Label = _ //TODO missingInputLabel is set to null?!
    @FXML
    private var sshNameField: TextField = _
    @FXML
    private var databaseNameField: TextField = _
    @FXML
    private var sshPasswdField: PasswordField = _
    @FXML
    private var databasePasswdField: PasswordField = _
    //Um einfach auf alle Text-/Passwortfelder referenzieren zu können
    private var textFields: Array[TextField] = Array[TextField](sshNameField, databaseNameField, sshPasswdField, databasePasswdField)
    var callbackWhenFinished: () =&gt; Unit = () =&gt; GTEVConnection.close() //Default: Simply close GTEVConnection
  
    def onKeyReleased_textField(ev: KeyEvent) { //TODO declare def as private and mark with @FXML?
      if (ev.getCode == KeyCode.ENTER) {
        login()
      } else {
        nameOrPasswdWrong.setVisible(false)
      }
    }
  
    def login() {
      checkInputOfTextFields()
      try {
        if (!missingInputLabel.isVisible) {
          GTEVConnection.createConnection(sshNameField.getText, sshPasswdField.getText, databaseNameField.getText, databasePasswdField.getText)
          callbackWhenFinished()
          sshNameField.getScene.getWindow.asInstanceOf[Stage].close()
        }
      } catch {
        case ex: SQLException =&gt;
          Logger.getLogger("LoginContollerLogger").log(Level.SEVERE, null, ex) //TODO Loggername durch Klassennamen ersetzen
          nameOrPasswdWrong.setVisible(true)
      }
    }
  
    private def checkInputOfTextFields() {
      var anyMissingInput: Boolean = false
      for (t: TextField &lt;- textFields) {
        if (t.getText.isEmpty) {
          anyMissingInput = true
          t.getStyleClass.add("missingInput")
        } else {
          t.getStyleClass.remove("missingInput")
        }
        missingInputLabel.setVisible(anyMissingInput)
      }
    }
  
    override def initialize(location: URL, resources: ResourceBundle) {
      nameOrPasswdWrong.setVisible(false)
      missingInputLabel.setVisible(false) //PROBLEM: NullPointerException
    }
  }
  </code></pre>
  
  <p>Tell me when i missed something or you want to know something else and thank you for your help.<br>
  Greetings Tracker</p>