繁体   English   中英

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

[英]Java - Null Pointer Exception when trying to add JLabel and/or JTextField to an array

我试图将一些Jlabels添加到数组中,以便稍后可以在程序中对其进行公开访问,但是当我尝试添加它们时,它给出了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的数量。
  • 中心是一个BoxLayout

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

谢谢!

您需要实例化数组。 for-loop之前添加以下行:

choiceLabels = new JLabel[number];
choices = new JTextField[number];

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

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

这是文档

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM