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

时间:2021-12-17 15:43:30

i am trying to add some Jlabels, to an array, so they can be accessed publicly later on in the program, but when i try to add them, it gives a NullPointerException.

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

The exact error is the following:

确切的错误如下:

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)

The code where the arrays are created is the following:

创建数组的代码如下:

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

The code where the JLabels, and the JTextFields are created and added to the array is the following:

创建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 is a JTextField, where a user would enter the amount of JLabels and JTextFields they want
  • num是一个JTextField,用户可以在其中输入他们想要的JLabel和JTextField的数量
  • center is a BoxLayout
  • 中心是一个BoxLayout

The error occurs on one of the last 4 lines of the method.

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

Thanks!

谢谢!

2 个解决方案

#1


1  

You need to instantiate the arrays. Add these lines before your for-loop:

您需要实例化数组。在for循环之前添加这些行:

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

#2


0  

try using ArrayList<JTextField> it is more convenient to use collections:

尝试使用ArrayList 使用集合更方便:

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

here is the documentation

这是文档

#1


1  

You need to instantiate the arrays. Add these lines before your for-loop:

您需要实例化数组。在for循环之前添加这些行:

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

#2


0  

try using ArrayList<JTextField> it is more convenient to use collections:

尝试使用ArrayList 使用集合更方便:

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

here is the documentation

这是文档