0

I am trying to print the contents of an array using System.out.print() but I encounter this error:

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 2, Size: 2
at java.util.ArrayList.rangeCheck(ArrayList.java:604)
at java.util.ArrayList.get(ArrayList.java:382)
at Polymorphism.actionPerformed(Polymorphism.java:196)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
at java.awt.Component.processEvent(Component.java:6270)
at java.awt.Container.processEvent(Container.java:2229)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
at java.awt.Container.dispatchEventImpl(Container.java:2273)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

The line the error is referring to is line 3:

 else if (e.getSource() == printArray) {
        for (int q=0; q<j; q++){
            System.out.print("Type: " + itemList.get(j).selectedType + "    ");
            System.out.print("Width: " + itemList.get(j).selectedWidth + "    ");
            System.out.print("Length: " + itemList.get(j).selectedLength + "    ");
    }
}

I don't have any debugger experience so I appreciate any help, thanks.

user2017027
  • 3
  • 1
  • 1
  • 2

7 Answers7

4

Change to .get(j) as Renjith says and you can also make sure to never end up in the situation you're in

instead of

for (int q=0; q<j; q++) {

do

for (int q=0; q<itemList.size(); q++) {

This way you cannot .get from anything larger than the size of the list/array

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
jelgh
  • 705
  • 1
  • 6
  • 22
2

You are referring to wrong index I guess.

change itemList.get(j) to itemList.get(q) ;

Renjith
  • 3,274
  • 19
  • 39
2

replace

        System.out.print("Type: " + itemList.get(j).selectedType + "    ");
        System.out.print("Width: " + itemList.get(j).selectedWidth + "    ");
        System.out.print("Length: " + itemList.get(j).selectedLength + "    ");

with

   System.out.print("Type: " + itemList.get(q).selectedType + "    ");
   System.out.print("Width: " + itemList.get(q).selectedWidth + "    ");
   System.out.print("Length: " + itemList.get(q).selectedLength + "    ");

The reason being you are iterating through the list with lookup value of List size it self.

The condition is breaking when loop index (which is always < listsize) is greater than listsize (which is 2 in this case)

TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
1

That is a blunder! Use:

else if (e.getSource() == printArray) {
        for (int q=0; q<j; q++){
            System.out.print("Type: " + itemList.get(q).selectedType + "    ");
            System.out.print("Width: " + itemList.get(q).selectedWidth + "    ");
            System.out.print("Length: " + itemList.get(q).selectedLength + "    ");
    }
}
Aakash Anuj
  • 3,773
  • 7
  • 35
  • 47
1

you are using this code to print the element

 itemList.get(j).selectedType

and you need to use this

 itemList.get(q).selectedType
nick_27
  • 66
  • 3
0

If j is the size of your item list, itemList.get(j) is going to give you an index out of bounds, as a zero-based index would only go to j - 1. It looks like you want itemList.get(q), from your code.

KevinH
  • 2,034
  • 1
  • 12
  • 12
0

It seems this is a pretty common mistake. If you are using any IDE like Eclipse, you should consider learning more about the Debug Perspective.

TechSpellBound
  • 2,505
  • 6
  • 25
  • 36