0

My idea: i wanted to have a textfield where a user can enter commands. All his inputs should be saved inside a list as a kind of history that the user can scroll through them with his arrowkeys.

My problem: i debugged my code multiple times, the commands are getting added to the history (textarea) and to the list propperly but inside my event handler all references to my textarea, textfield and list are always null.

My code:

public class Controller extends Application implements Initializable {

@FXML
private TextField commandInput;
@FXML
private TextArea commandHistory;

private LinkedList<String> commandList = new LinkedList<>();


@Override
public void start(Stage primaryStage) throws Exception {
    Parent root = FXMLLoader.load(getClass().getResource("layout.fxml"));
    primaryStage.setTitle("Airport");
    Scene scene = new Scene(root, 700, 275);
    primaryStage.setScene(scene);
    primaryStage.show();

    scene.addEventFilter(KeyEvent.KEY_PRESSED, event -> {

        if (!commandList.isEmpty()) {


            switch (event.getCode()) {
                case UP:
                    commandInput.setText(commandList.getFirst());
                    commandList.addLast(commandList.getFirst());
                    commandList.removeFirst();
                    break;

                case DOWN:
                    commandInput.setText(commandList.getLast());
                    commandList.addFirst(commandList.getLast());
                    commandList.removeLast();
                    break;
            }
        }
        System.out.println("Pressed: " + event.getCode());

    });
}

private boolean parseString(String input) {

    //TODO: Write Parser
    return true;
}

public void enterPressed(ActionEvent actionEvent) {
    String input = commandInput.getText();

    if (parseString(input)) {
        addCommandToHistory(input);
        commandList.add(input);
        System.out.println(commandList.getFirst());
        commandInput.setText("");
    } else {
        addCommandToHistory("input is no command");
    }
XaNNy0
  • 169
  • 1
  • 8
  • Can you show the code of `layout.fxml`? – Gnas Dec 05 '18 at 09:23
  • You're creating 2 instances of your `Application` class: one that is launched (`start` is called by the toolkit) and one that is used as controller for your fxml, I assume. Those are different instances without any kind of connection (except for sharing a type). – fabian Dec 05 '18 at 12:33

0 Answers0