简体   繁体   English

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

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

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

You need to instantiate the arrays. 您需要实例化数组。 Add these lines before your for-loop : for-loop之前添加以下行:

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

try using ArrayList<JTextField> it is more convenient to use collections: 尝试使用ArrayList<JTextField> ,使用集合更方便:

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

here is the documentation 这是文档

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

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