将Textfield值放入数组,ArrayIndexOutOfBoundsException

时间:2016-02-27 01:48:20

标签: java arrays jtextfield

我有两个名为field1和field2的JTextField,目标是键入一个名称,当我点击返回时,将这些值存储在nameArray []中的0和1位。

我不确定原因是我的动作监听器的逻辑还是我声明我的数组的方式。如果是其中任何一个......

数组声明如下,就在我的类声明下面:

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

这是我的动作监听器和field1初始化:

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);
    }
  }); 

强制性堆栈:

线程“AWT-EventQueue-0”中的异常java.lang.ArrayIndexOutOfBoundsException:0     在tests.TwoPlayer $ 1.actionPerformed(TwoPlayer.java:37)     在javax.swing.JTextField.fireActionPerformed(JTextField.java:508)     在javax.swing.JTextField.postActionEvent(JTextField.java:721)     在javax.swing.JTextField $ NotifyAction.actionPerformed(JTextField.java:836)     在javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1663)     在javax.swing.JComponent.processKeyBinding(JComponent.java:2882)     在javax.swing.JComponent.processKeyBindings(JComponent.java:2929)     在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)     在java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

1 个答案:

答案 0 :(得分:2)

这是初始化阵列的方式。您目前有private String[] nameArray = {};,会创建一个长度为0的新String[]并解释IndexOutOfBoundsException

您想要的是private String[] nameArray = new String[2];,它会初始化一个包含两个字符串的String[]