0

Na是一个参数。我确信这个参数是正确的。我已经声明了一个 Na JTextField 数组,我想使用其中的一个。但是当我运行我的代码时,有一个异常......

那是我的代码:

import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

class Panneau_fenetre1A extends JPanel                      
{   
    public Panneau_fenetre1A(int Na)
    {
        JTextField[] allField = new JTextField [Na];
        for(int i = 0;i<Na;i++)
        {
            allField[i].setText(String.valueOf(0));
            allField[i].setBounds(150, 350+i*25,20, 20);
        }
    }

}

那是个例外:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Panneau_fenetre1A.<init>(Panneau_fenetre1A.java:18)
    at Cadre_fenetre1A.<init>(Cadre_fenetre1A.java:13)
    at Panneau_fenetre$ecout1.actionPerformed(Panneau_fenetre.java:64)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2012)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2335)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:404)
    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:6268)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
    at java.awt.Component.processEvent(Component.java:6033)
    at java.awt.Container.processEvent(Container.java:2045)
    at java.awt.Component.dispatchEventImpl(Component.java:4629)
    at java.awt.Container.dispatchEventImpl(Container.java:2103)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4633)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4297)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4227)
    at java.awt.Container.dispatchEventImpl(Container.java:2089)
    at java.awt.Window.dispatchEventImpl(Window.java:2517)
    at java.awt.Component.dispatchEvent(Component.java:4455)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:649)
    at java.awt.EventQueue.access$000(EventQueue.java:96)
    at java.awt.EventQueue$1.run(EventQueue.java:608)
    at java.awt.EventQueue$1.run(EventQueue.java:606)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
    at java.awt.EventQueue$2.run(EventQueue.java:622)
    at java.awt.EventQueue$2.run(EventQueue.java:620)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:619)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
4

2 回答 2

2

You've failed to properly initialize your array. Therefore, allField[i] = null for all i < Na. Hence the NullPointerException. The following will work,

for(int i = 0;i < Na;i++)
{
    JTextField textField = new JTextField(String.valueOf(0));
    textField.setBounds(150, 350 + i * 25, 20, 20);
    allField[i] = textField;
}
于 2012-08-07T18:21:12.180 回答
0

这条线...

JTextField[] allField = new JTextField [Na];

初始化一个新的 ARRAY,它将保存 JTextField 对象。数组中有 -Na- 空格,但每个空格仍然是空的。您仍然需要创建 JTextField 对象才能进入空数组空间。

像这样的东西应该工作......

for(int i = 0;i < Na;i++)
{
    allField[i] = new JTextField(String.valueOf(0));
    allField[i].setBounds(150, 350 + i * 25, 20, 20);

}
于 2012-08-07T18:26:04.190 回答