简体   繁体   中英

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.

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:

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
  • center is a BoxLayout

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

Thanks!

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

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

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

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

here is the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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