0

I'am very confused on why I am getting java.lang.ArrayIndexOutOfBoundsException in my code (using JavaFx), the following method saves the information entered on a local TXT file, splitting it in 3:

public void saveToFile() throws IOException {
    Path path = Paths.get(ticketFileName);      //C:\\temp\\ticketlog.txt
    BufferedWriter bw = Files.newBufferedWriter(path);
    try {
        Iterator<Ticket> iter = ticketList.iterator();
        while (iter.hasNext()) {
            Ticket item = iter.next();
            bw.write(String.format("%s->\t%s->\t%s", item.getCa(), item.getSummary(), item.getNotes()));
            bw.newLine();
        }

    } finally {
        if (bw != null) {
            bw.flush();
        }
    }
}

on the initialize method the read method is:

Path path = Paths.get(ticketFileName);
try {
        // read file and put on the listview
        BufferedReader br = Files.newBufferedReader(path);   //C:\\temp\\ticketlog.txt
        while ((input = br.readLine()) != null) {
            String[] itemPieces = input.split("->\t");
                String ca = itemPieces[0];
                String summary = itemPieces[1];
                String notes = itemPieces[2];
                Ticket ticket = new Ticket(ca, summary, notes);
                ticketList.add(ticket);

        }

    } catch (IOException e) {

        e.printStackTrace();
    }

and normally I have no problem with this, however, I have a Combobox which inserts text into the TextArea (which is the itemPieces[2]):

comboKb.getSelectionModel().selectedIndexProperty().addListener((v, oldValue, newValue) -> {
        txtKb.setText(comboKb.getValue().getLink());
        txtKb.setTooltip(new Tooltip(txtKb.getText()));

        txtaGetInfo.setText(txtaComplete + "\n\n\n" + comboKb.getValue().getLink());
    });

Selecting any item it gives me the exception:

    javafx.fxml.LoadException: 
/D:/Arquivo%20micro/alisson/FACUL/Facul/Orienta%c3%a7%c3%a3o%20a%20objetos/Eclipse/JavaFxTicketGenerator/bin/application/mainwindows.fxml

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at application.Main.start(Main.java:19)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ArrayIndexOutOfBoundsException: 2
    at application.Controller.initialize(Controller.java:270)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548)
    ... 17 more

even through it gives me the exception, it looks normal on the Txt file.

If I delete the "\n" spaces it normally works, tried then to change the split case to anything else, still doesn't work.

This is driving me crazy, can someone please shed me some light?

Thanks a lot!

sylleryum
  • 130
  • 1
  • 9

1 Answers1

1

You should test to make sure that this code

String[] itemPieces = input.split("->\t");

actually returns an array of 3 or more Strings

 if (itemPieces.length >= 3) {
   String ca = itemPieces[0];
   String summary = itemPieces[1];
   String notes = itemPieces[2];
   ...
 }

Consider that you may have blanks lines or other invalid data

see bw.newLine();

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • In short, it's a terrible idea if your String source allows line breaks and your reader reads by line. – Jai Jul 05 '17 at 05:13
  • @Jai sometimes it can not be helped, especially if a thrid-party person can get her hands on your config or data files – Scary Wombat Jul 05 '17 at 05:15