2016-02-27 86 views
0

我有兩個名爲field1和field2的JTextFields,目標是鍵入一個名稱,當我點擊返回時,將這些值存儲在位置0和1的nameArray []中。將Textfield值放入數組中,ArrayIndexOutOfBoundsException

我不確定原因是我的動作偵聽器的邏輯還是我聲明數組的方式。如果任這些東西

數組聲明像這樣,右下方我的類聲明:

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

這是我的動作偵聽器和字段1初始化:

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

場2:

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

強制性堆棧:

在線程0

例外 「AWT-EventQueue的 - 0」 java.lang.ArrayIndexOutOfBoundsException:0 在tests.TwoPlayer $ 1.actionPerformed(TwoPlayer.java:37) 在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) ) 在javax.swing.JComponent.processKeyBinding(JComponent.java:2882) 在javax.swing.JComponent.processKeyBindings(JComponent.java:2929) 在javax.swing.JComponent.processKeyEvent(JComponent.java:2845) 在java.awt.Component.processEve nt(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) 在java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusManager.java:1074) 在java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusManager.java:945) 在java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFo cusManager.java:771) 在java.awt.Component.dispatchEventImpl(Component.java:4762) 在java.awt.Container.dispatchEventImpl(Container.java:2294) 在java.awt.Window.dispatchEventImpl(窗口。 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)位於java.security.ProtectionDomain上的 $ JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76) 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.ProtectionDomain $ JavaSecurityAccessImpl java.security.AccessController.doPrivileged(Native Method) 。doIntersectionPrivilege(ProtectionDomain.java:76) 在java.awt.EventQueue.dispatchEvent(EventQueue.java:728) 在java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) 在java.awt.EventDispatchThread.pumpEventsForFilter( EventDispatchThread.java:116) 在java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) 在java.awt.EventDispatchThread.pumpEvents(EventDispatchThread。 (EventDispatchThread.java:82)

回答

2

這是您初始化數組的方式。您目前有private String[] nameArray = {};,它會創建長度爲0的新String[]並解釋IndexOutOfBoundsException

你想要的是private String[] nameArray = new String[2];,它初始化一個String[],它將包含兩個字符串。

+0

剛剛意識到...謝謝 – CodeBoy

+0

已修復,但我現在在「字符串[2]」的「字符串」部分出現錯誤,提示我創建了一個常量字符串或字段字符串 – CodeBoy

+0

這是我的錯。您需要在更新後的答案中使用前面的'new'關鍵字。 'private String [] nameArray = new String [2];' –