Okay, so I have a ListView populated by an Array, which is populated by a text file. I also have 3 TextBoxes which are changed to different values in multiple Arrays using the Selected Index. My problem occurs when reloading the ListView after saving the text file. The values from the TextBoxes are used in the text file.
What happens is, the index is being changed since the ListView is being cleared, which fires the changed method. Since the ListView is empty now, the Selected Index is -1 which is why I get an Array Index Out Of Bounds Exception shown below.

Index Changed Listener

    SceneController.bugList.getSelectionModel().selectedItemProperty()
            .addListener(new ChangeListener<String>()
                {
                    @Override
                    public void changed(
                            ObservableValue<? extends String> observable,
                            String oldValue, String newValue)
                    {
                        SceneController.textID.setText(Integer

                                    .toString(IDs[SceneController.bugList //ARRAY OUT OF BOUNDS -1
                                            .getSelectionModel().getSelectedIndex()]));
                        SceneController.textName.setText(names
                                .get(SceneController.bugList
                                        .getSelectionModel().getSelectedIndex()));
                        SceneController.textDescription
                                .setText(descriptions[SceneController.bugList
                                        .getSelectionModel().getSelectedIndex()]);
                    }
                });

LoadList

private static void loadList() throws IOException
    {
        SceneController.bugList.setItems((ObservableList<String>) clearList);
        currentID = 0;
        InputStream reader = BugTracker.class
                .getResourceAsStream("/bugList.txt");
        br = new BufferedReader(new InputStreamReader(reader));

        String str;
        while ((str = br.readLine()) != null)
        {
            String[] strArr = new String[3];
            strArr = str.split(", ");

            IDs[currentID] = Integer.parseInt(strArr[0]);
            names.add(strArr[1]);
            descriptions[currentID] = strArr[2];

            currentID++;
        }

        SceneController.bugList.setItems((ObservableList<String>) names);
    }

I have attempted to fix this using a boolean to check if the list was being loaded and only fire the stuff in the changed method while it wasn't loading, but that just caused the program to hang when I tried changing index's.

Any possible solutions I could try to solve this issue?

Simply test for a -1 selected index before trying to use it!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.