I have an application in JavaFX with the .FXML file, and I add a button to the scene. Then I tried to add an accelerator to it, but when it launches it, it throws a NullPointerException. Why it does not work and how to solve it.
@FXML Button addQuickNote; @FXML public void handlequickNote(ActionEvent e) { String text = SampleController.getSelectedText(); if (text != null) { SampleController.profileManager.insertNote(DataParser.getNote(text)); } } @Override public void initialize(URL url, ResourceBundle rb) { addQuickNote.getScene().getAccelerators().put(new KeyCodeCombination(KeyCode.Q, KeyCombination.SHORTCUT_DOWN), new Runnable() { @Override public void run() { addQuickNote.fire(); } }); }
My.fxml is quite complicated because it contains the whole module for my application, so I only insert the row with the button. The button is in the toolbar.
<Button fx:id="addQuickNote" mnemonicParsing="false" onAction="#handlequickNote" prefWidth="77.0" text="Z tekstu" />
I am loading .fxml as part of the main scene. I am doing this with this code.
try { panel = FXMLLoader.load(getClass().getResource("Notes.fxml")); } catch (IOException ex) { showErrorDialog ....; } rightPanel.getChildren().add(panel); mainPanel.setRight(rightPanel);
source share