Tack
Tack

Reputation: 121

Putting Textfield value into array, ArrayIndexOutOfBoundsException

I have two JTextFields called field1 and field2, the goal is to type in a name and when I hit return, have those values become stored in nameArray[] at positions 0 and 1.

I'm not sure whether the cause is the logic of my action listener or the way I am declaring my array. If it is either of those things...

The array is declared like so, right below my class declaration:

public class TwoPlayer{
private String[] nameArray = {};

Here is my action listener and field1 initialization:

JTextField field1 = new JTextField("Left name");
    field1.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String lValue = field1.getText();
    String leftValue = String.valueOf(lValue);
    nameArray[0] = (leftValue);
    }
  });  

field2:

JTextField field2 = new JTextField("Right name");   
    field2.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String rValue = field2.getText();
    String rightValue = String.valueOf(rValue);
    nameArray[1] = (rightValue);
    }
  }); 

Obligatory Stack:

Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 0 at tests.TwoPlayer$1.actionPerformed(TwoPlayer.java:37) at javax.swing.JTextField.fireActionPerformed(JTextField.java:508) at javax.swing.JTextField.postActionEvent(JTextField.java:721) at javax.swing.JTextField$NotifyAction.actionPerformed(JTextField.java:836) at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663) at javax.swing.JComponent.processKeyBinding(JComponent.java:2882) at javax.swing.JComponent.processKeyBindings(JComponent.java:2929) at javax.swing.JComponent.processKeyEvent(JComponent.java:2845) at java.awt.Component.processEvent(Component.java:6312) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4891) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4713) 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:4762) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Window.dispatchEventImpl(Window.java:2750) at java.awt.Component.dispatchEvent(Component.java:4713) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 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)

Upvotes: 0

Views: 86

Answers (1)

George Mulligan
George Mulligan

Reputation: 11923

It is the way you are initializing the array. You currently have private String[] nameArray = {}; which creates a new String[] with length 0 and explains the IndexOutOfBoundsException.

What you want instead is private String[] nameArray = new String[2]; which initializes a String[] that will hold two strings.

Upvotes: 2

Related Questions