gpt4 book ai didi

java - 尽管没有访问任何越界索引,但出现异常 "ArrayOutOfBounds:1"

转载 作者:行者123 更新时间:2023-12-01 13:48:27 26 4
gpt4 key购买 nike

虽然这段代码在没有添加 GUI 的情况下工作正常。我现在遇到以下异常。 (我读到此类异常的 SCCE 发布了整个堆栈跟踪以及堆栈跟踪第一行的代码。我希望它很清楚。)

错误:

   Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at generaterpt.TransactionDetails.ReadFromFile(TransactionDetails.java:90)
at generaterpt.DMS.GenerateReport(DMS.java:25)
at report.Monthly.genButtonActionPerformed(Monthly.java:95)
at report.Monthly.access$000(Monthly.java:20)
at report.Monthly$1.actionPerformed(Monthly.java:55)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
at java.awt.Component.processMouseEvent(Component.java:6263)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6028)
at java.awt.Container.processEvent(Container.java:2041)
at java.awt.Component.dispatchEventImpl(Component.java:4630)
at java.awt.Container.dispatchEventImpl(Container.java:2099)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4574)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4238)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4168)
at java.awt.Container.dispatchEventImpl(Container.java:2085)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4460)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

代码:

try
{
File file = new File("TransactionDetails.txt");

FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = "";
while((line = br.readLine()) != null)
{
String [] tokens = line.split(",");
String [] modeltoken = tokens[0].split("=");
String [] nametoken = tokens[1].split("=");
String [] monthtoken = tokens[2].split("=");
String [] yeartoken = tokens[3].split("=");


String model = (""+modeltoken[1]);
}

最后一行有错误。

最佳答案

您访问数组元素的唯一一行是:

 String model = (""+modeltoken[1]);

因此,可以假设 modeltoken 的元素少于 2 个,并且文件中的数据有错误。

这会导致异常。我建议您添加错误检查代码:

String model = "No Data In File";

if(modeltoken.length < 2)
System.err.println("modeltoken has less than 2 elements. Size is:"+String.valueOf(smodeltoken.length));
else
model = (""+modeltoken[1]);

关于java - 尽管没有访问任何越界索引,但出现异常 "ArrayOutOfBounds:1",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20163493/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com