0

I have the following methods in my javafx application:

@FXML
    private void addNewWorkerButton(ActionEvent event) {
        String name = newWorkerNameTextField.getText();
        int wage = Integer.parseInt(newWorkerWageTextField.getText());
        allWorks.add(new Work(new Worker(getNextId(), name, wage)));
        new FileHandling().writeDataToFileAsJSON(allWorks); //I save the data in JSON
        setNamesChoiceBoxes();
        }
    }

private void setNamesChoiceBoxes() {
        ArrayList<String> names = new ArrayList<>();
        ArrayList<Worker> workers = new FileHandling().getWorkersDataFromJSONFile(); //I read all Workers objects from JSON

        for (Worker i : workers) {
            String tmp = i.getName() + " (" + i.getId() + ")";
            names.add(tmp);
        }

        ArrayList<String> searchNames = new ArrayList<>(names);
        searchNames.add(0, "All");
        ObservableList<String> search = FXCollections.observableList(searchNames);
        searchNameChoiceBox.setItems(search);
        searchNameChoiceBox.getSelectionModel().selectFirst();
    }
private void search() {
        int workerID = 0;                   
        String idString = "" + searchNameChoiceBox.getSelectionModel().getSelectedItem();
        System.out.println("Search: " + searchNameChoiceBox.getSelectionModel().getSelectedItem());
        if (!idString.equals("All")) {
                workerID = Integer.parseInt(idString.substring(idString.indexOf("(") + 1, idString.indexOf(")")));
        }

    }

@Override
    public void initialize(URL url, ResourceBundle rb) {
searchNameChoiceBox.setOnAction((event) -> {
            search();
        });
}

I have a choice box that contains all workers that my JSON file have (searchNameChoiceBox). When I add a new worker, I call the setNamesChoiceBoxes() method what reads all workers out of JSON file and refreshes the choice box. The "setOnAction" will call the search() method what gets a null here:

System.out.println("Search: " + searchNameChoiceBox.getSelectionModel().getSelectedItem());

Somehow at this line gets a "null" what should not be possible since in the setNamesChoiceBoxes() method I select the first option ("searchNameChoiceBox.getSelectionModel().selectFirst();")

Search: All // I started the application

Search: null // I added a new worker

Exception in thread "JavaFX Application Thread" java.lang.StringIndexOutOfBoundsException: String index out of range: -1 at java.lang.String.substring(Unknown Source) at controller.Control.search(Control.java:335) at controller.Control.lambda$4(Control.java:440) at ...

The Control.java:335 is this line:

workerID = Integer.parseInt(idString.substring(idString.indexOf("(") + 1, idString.indexOf(")")));

The Control.java:440 is the search() call in initialize() method.

I don't get a null when I work with any array.

laklaklak
  • 53
  • 1
  • 6
  • Possible duplicate of [What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it?](https://stackoverflow.com/questions/5554734/what-causes-a-java-lang-arrayindexoutofboundsexception-and-how-do-i-prevent-it) – toastedDeli Oct 18 '19 at 18:18
  • Read the exception message. Look at the documentation for `indexOf()`. – toastedDeli Oct 18 '19 at 18:30
  • Your exception says: `String index out of range: -1`. The `indexOf` documentation says `the index of the first occurrence of the specified substring` **or -1 if there is no such occurrence.** I'm betting there's no such occurrence. – toastedDeli Oct 18 '19 at 18:37
  • I would check 3 things. 1. I think `initialize` needs the `@FXML` annotation. 2. Make sure `search` is not being called anywhere before `initialize`. 3. Make sure there is actually an item selected and `getSelectedItem` is not returning null. – SephB Oct 18 '19 at 18:38
  • What happens if your string does not contain ")"? – Tobb Oct 18 '19 at 18:46
  • stringId becomes null, that is why the Integer.parseInteger gets an exception. Because "null" does not contain whatever I need. The question is: why stringId becomes null. – laklaklak Oct 18 '19 at 18:49
  • `.getSelectedItem()` returns null. Could it be because no item is selected? – Tobb Oct 18 '19 at 19:14
  • if `idString` is becoming `"null"`... then that is because `.getSelectedItem()` is returning null..... `"" + null = "null"` – RobOhRob Oct 18 '19 at 19:39
  • If `idString` was null, the exception would be a null pointer exception when trying to call `substring` on `idString`. Also, `idString` is dereferenced in the line before where there exception is thrown. If `idString` was null, the exception would be on that line. – toastedDeli Oct 18 '19 at 19:43
  • " searchNameChoiceBox.getSelectionModel().selectFirst();" - it should not be null. It should be "All" – laklaklak Oct 18 '19 at 20:06
  • [mcve] please .. – kleopatra Oct 20 '19 at 14:21

0 Answers0