Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/396.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
&引用;java.lang.ArrayIndexOutOfBoundsException“;检查尺寸是否正确_Java_Exception_Javafx - Fatal编程技术网

&引用;java.lang.ArrayIndexOutOfBoundsException“;检查尺寸是否正确

&引用;java.lang.ArrayIndexOutOfBoundsException“;检查尺寸是否正确,java,exception,javafx,Java,Exception,Javafx,我对为什么在我的代码中获取java.lang.ArrayIndexOutOfBoundsException(使用JavaFx)感到非常困惑,下面的方法将输入的信息保存在本地TXT文件中,并将其拆分为3: public void saveToFile() throws IOException { Path path = Paths.get(ticketFileName); //C:\\temp\\ticketlog.txt BufferedWriter bw = File

我对为什么在我的代码中获取java.lang.ArrayIndexOutOfBoundsException(使用JavaFx)感到非常困惑,下面的方法将输入的信息保存在本地TXT文件中,并将其拆分为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();
        }
    }
}
通常我对此没有问题,但是,我有一个组合框,可以将文本插入文本区域(即itemPieces[2]):

选择任何项目都会出现异常:

    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
即使它给了我一个异常,它在Txt文件上看起来也很正常

如果我删除了正常工作的“\n”空格,然后尝试将拆分的大小写更改为其他任何内容,仍然不起作用

这让我快发疯了,有人能给我点启示吗


非常感谢

您应该测试以确保此代码

String[] itemPieces = input.split("->\t");
实际返回3个或更多字符串的数组

 if (itemPieces.length >= 3) {
   String ca = itemPieces[0];
   String summary = itemPieces[1];
   String notes = itemPieces[2];
   ...
 }
请考虑您可能有空白行或其他无效数据


参见
bw.newLine()

您可以使用调试器吗?代码乍一看是正常的,但调试器会告诉你发生了什么。第270行是什么?简言之,如果字符串源允许换行,而你的读者逐行读取,这是一个糟糕的主意。@Jai有时这是帮不上忙的,特别是如果第三方的人可以得到你的配置或数据文件
String[] itemPieces = input.split("->\t");
 if (itemPieces.length >= 3) {
   String ca = itemPieces[0];
   String summary = itemPieces[1];
   String notes = itemPieces[2];
   ...
 }