Java - Null Pointer尝试将JLabel和/或JTextField添加到数组时出现异常

时间:2014-02-20 21:32:50

标签: java arrays swing jlabel jtextfield

我正在尝试将一些Jlabel添加到数组中,以便稍后可以在程序中公开访问它们,但是当我尝试添加它们时,它会产生NullPointerException。

确切的错误如下:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at Questionnaire.choices(Questionnaire.java:337)
at Questionnaire$1.insertUpdate(Questionnaire.java:97)
at javax.swing.text.AbstractDocument.fireInsertUpdate(Unknown Source)
at javax.swing.text.AbstractDocument.handleInsertString(Unknown Source)
at javax.swing.text.AbstractDocument.insertString(Unknown Source)
at javax.swing.text.PlainDocument.insertString(Unknown Source)
at javax.swing.text.AbstractDocument.replace(Unknown Source)
at javax.swing.text.JTextComponent.replaceSelection(Unknown Source)
at javax.swing.text.DefaultEditorKit$DefaultKeyTypedAction.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

创建数组的代码如下:

public static JTextField[] choices; 
public static JLabel[] choiceLabels;

创建JLabel和JTextField并将其添加到数组的代码如下:

public static void choices()
{
    center.removeAll();
    center.add(no);
    center.add(num);

    int number = Integer.parseInt(num.getText());

    if(Integer.toString(number) != "")
    {
        FileWindow.createWindow.setSize(800,(380 + (number * 50)));
        for(int i = 0; i < number; i++)
        {
            String n = Integer.toString(i);
            JLabel choiceL = new JLabel("Choice " + (n + 1) + ":");
            JTextField choice = new JTextField();

            System.out.println(choiceL.toString());

            choiceLabels[i] = choiceL;
            choices[i] = choice;
            center.add(choiceL);
            center.add(choice);
        }
    }
}
  • num是一个JTextField,用户可以输入他们想要的JLabel和JTextField的数量
  • center是BoxLayout

该方法的最后4行之一发生错误。

谢谢!

2 个答案:

答案 0 :(得分:1)

您需要实例化数组。在for-loop

之前添加这些行
choiceLabels = new JLabel[number];
choices = new JTextField[number];

答案 1 :(得分:0)

尝试使用ArrayList<JTextField>使用集合更方便:

ArrayList<JTextField> choiceLabels = new ArrayList<JTextField>();
al.add(choiceLabel);
al.add(choiceLabel);

这是documentation