I am trying to make a text adventure in java gui. I have just started this project and I am trying to get the game to realize when a player types "get" + the item. I want it to take the item out of the current ArrayList it is in and add it to the inventory screen I have made. Currently everything works correctly except for when I type a different command like "look". All it should show then is the room description, but it hows the description and picks up the item and puts it in the inventory screen. Then I get this error.
Exception in thread "AWT-EventQueue-0" java.util.ConcurrentModificationException
at java.util.ArrayList$Itr.checkForComodification(ArrayList.java:901)
at java.util.ArrayList$Itr.next(ArrayList.java:851)
at Gamegui.processCommands(Gamegui.java:327)
at Gamegui.playerInputKeyPressed(Gamegui.java:359)
at Gamegui.access$200(Gamegui.java:14)
at Gamegui$3.keyPressed(Gamegui.java:171)
at java.awt.Component.processKeyEvent(Component.java:6483)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2829)
at java.awt.Component.processEvent(Component.java:6302)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1954)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManager.java:806)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.java:771)
at java.awt.Component.dispatchEventImpl(Component.java:4752)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
at java.awt.EventQueue.access$400(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:697)
at java.awt.EventQueue$3.run(EventQueue.java:691)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:719)
at java.awt.EventQueue$4.run(EventQueue.java:717)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
BUILD SUCCESSFUL (total time: 10 seconds)
Here is my relative code.
processCommands
public void processCommands(String input)
{
ArrayList<String> items = currentRoom.getItems();
if(input.equals("look"))
{
gameScreen.append("\n" + currentRoom.getDescription());
}
for(String item : items)
{
if(input.equals("get " + item));
{
items.remove(item);
gameScreen.append("\nYou pick up the " + item);
invScreen.append("\n" + item);
playerInput.setText("");
}
}
}
playerInputKeyPressed
private void playerInputKeyPressed(java.awt.event.KeyEvent evt) {
if(evt.getKeyCode() == evt.VK_ENTER)
{
getInput();
if(validCommands(inputText))
{
processCommands(inputText);
}
else
{
gameScreen.append("\nI don't Understand");
}
playerInput.setText("");
try
{
JScrollBar sb = gameScreenPlace.getVerticalScrollBar();
sb.setValue(sb.getMaximum());
}
catch(java.lang.NullPointerException err)
{
}
}
}
Can someone please help me fix this?