获取异常“ArrayOutOfBounds:1”虽然它没有访问任何超出范围的索引

时间:2013-11-23 14:24:36

标签: java arrays exception user-interface stack-trace

虽然没有添加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]);
}

错误发生在最后一行。

1 个答案:

答案 0 :(得分: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]);