gpt4 book ai didi

java - 索引越界异常 : cant figure out what is happening? 学校作业

转载 作者:行者123 更新时间:2023-12-01 05:19:10 39 4
gpt4 key购买 nike

这是我第一次使用 stackoverflow 来解决我需要问的问题,所以希望我能得到一些帮助。这是我正在做的一项学校作业,虽然我已经上交了它并且很快就会大幅更改代码,但我想首先找出问题所在。这基本上是一个购买历史记录程序,其中存储了过去的购买记录,并且可以通过“下一步”和“后退”按钮作为收据进行访问。没有文件访问,所有数据都是随机生成的。随机数据生成并不是作业的要求,但我想将其作为一种学习体验。

问题来了,这个错误是怎么回事?

    Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 9, Size: 9
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at farmmark.Storage.getItems(Storage.java:36)
at farmmark.Gui.buttonActionNext(Gui.java:415)
at farmmark.Gui$ButtonHandler.actionPerformed(Gui.java:632)
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:6290)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6055)
at java.awt.Container.processEvent(Container.java:2039)
at java.awt.Component.dispatchEventImpl(Component.java:4653)
at java.awt.Container.dispatchEventImpl(Container.java:2097)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4575)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4236)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4166)
at java.awt.Container.dispatchEventImpl(Container.java:2083)
at java.awt.Window.dispatchEventImpl(Window.java:2482)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:648)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:607)
at java.awt.EventQueue$1.run(EventQueue.java:605)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:621)
at java.awt.EventQueue$2.run(EventQueue.java:619)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:618)
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)
BUILD SUCCESSFUL (total time: 6 seconds)

代码很多,可能太多了,无法在这里发布,所以这里是源代码的链接: http://www.mediafire.com/?3ogc334vwh6t7k6

以下是发生错误的代码点:

在 farmmark.Storage.getItems(Storage.java:36)

public static String[] getItems(int select)
{
return itemList.get(select);
}

在 farmmark.Gui.buttonActionNext(Gui.java:415)

String items[] = Storage.getItems(counter);

在 farmmark.Gui$ButtonHandler.actionPerformed(Gui.java:631)

else if(sel.equals("Next"))
{
jtxtItems.setText(null);
jtxtPrices.setText(null);
jtxtSkus.setText(null);
jtxtCase.setText(null);
jtxtVend.setText(null);
panel.removeAll();
buttonActionNext();
{

任何帮助都会很棒,请记住我只用 java 编程了 8 周:P。提前致谢!

最佳答案

查看位置:

at farmmark.Storage.getItems(Storage.java:36)

方法如下:

public static String[] getItems(int select)
{
return itemList.get(select);
}

select 的值小于零或大于(itemList.length-1)

在调试器中运行,在该行设置断点,然后查看发生了什么。

我没有看到 itemList 或 select 的设置位置。也看看这些方法。

只是好奇 - 为什么该方法是静态的?

关于java - 索引越界异常 : cant figure out what is happening? 学校作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10694528/

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