Domanda

This is my first time using stackoverflow for a question that I need to ask, so hopefully I can get some help. This is a school assignment that I am working on, and although I have already turned it in and am going to change the code pretty drastically soon, I would like to figure out what is wrong first. This is basically a purchase history program, where the past purchases are stored and can be accessed as receipts with a 'next' and 'back' button. There is no file access, and all the data is generated randomly. The random data generation wasn't a requirement of the assignment, but I wanted to do it that way as a learning experience.

On to the question, what is going on with this error?

    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)

There is a lot of code, probably too much to post here, so here is a link to the source: http://www.mediafire.com/?3ogc334vwh6t7k6

Here are the spots of code where the error occurs:

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

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

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

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

at 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();
        {

Any help would be great, keep in mind I have only been programming in java for 8 weeks :P. Thanks in advanced!

È stato utile?

Soluzione

Here's where to look:

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

Here's the method:

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

The value for select is either less than zero or greater than (itemList.length-1).

Run in a debugger, set a breakpoint at that line, and see what's up.

I don't see where either itemList or select is set. Take a look at those methods as well.

Just curious - why is that method static?

Altri suggerimenti

You need to look at farmmark.Gui$ButtonHandler.actionPerformed(Gui.java:632) and see that value is passed to Gui.buttonActionNext(), as that value is passed on to Storage.getItems() which ends up naming an index that is larger than the size of the list (-1).

It is also possible that there are no items in the list (Storage.itemList) to begin with, in which case you will get this exception, regardless of the passed-on value

Well, it seems you have a lot to learn. How about you start by wrapping a little check before you try to access you list item like this:

public static String[] getItems(int select)
{
    if (select < itemList.size()) {
        return itemList.get(select);
    } else {
        throw RuntimeException("We cannot access an item that does not exist");
    }
}

Then, try to find out why the calling code is trying to access the item that does not exist. Read the stack trace, it will tell you exactly where the problem is.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top