0

I'm hoping this is an easy question. I have a JComboBox with the choices of 0, 1, 2, 3,...10. Depending on what number is selected in the JComboBox, I want my GUI to add a JLabel and a JTextField. So if the number 3 is chosen, the GUI should add 3 JLabels and 3 JTextFields. and so forth.

I'm using an array of JLabels and JTextFields to accomplish this, but I am getting a null pointer exception at runtime, and no labels or fields are being added.

Code:

private void createComponents()
{
    //Create Action Listeners
    ActionListener comboListener = new ComboListener();

    //Create Components of the GUI
    parseButton = new JButton("Parse Files");
    parseButton.addActionListener(comboListener);

    numberLabel = new JLabel("Number of Files to Parse: ");
    String[] comboStrings = { "","1", "2","3","4","5","6","7","8","9","10" };
    inputBox = new JComboBox(comboStrings);
    inputBox.setSelectedIndex(0);

    fieldPanel = new JPanel();        
    fieldPanel.setLayout(new GridLayout(2,10));

    centerPanel = new JPanel();
    centerPanel.add(numberLabel);
    centerPanel.add(inputBox);      

    totalGUI = new JPanel();
    totalGUI.setLayout(new BorderLayout());
    totalGUI.add(parseButton, BorderLayout.SOUTH);
    totalGUI.add(centerPanel, BorderLayout.CENTER);        

    add(totalGUI);
}

ActionListener Code:

public void actionPerformed(ActionEvent e)
{          
        JTextField[] fileField = new JTextField[inputBox.getSelectedIndex()];
        JLabel[] fieldLabel = new JLabel[inputBox.getSelectedIndex()];
        for(int i = 0; i < fileField.length; i++)
        {
            fieldLabel[i].setText("File "+i+":");  //NULL POINTER EXCEPTION HERE
            fieldPanel.add(fieldLabel[i]);         //NULL POINTER EXCEPTION HERE
            fieldPanel.add(fileField[i]);
        }
        centerPanel.add(fieldPanel);
        repaint();
        revalidate();
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Justiciar
  • 356
  • 1
  • 3
  • 20
  • `fieldLabel` and `fileField` don't contain any elements, they only have space for them. You need to assign a new value to each element before you can interact with them – MadProgrammer Nov 06 '17 at 00:38
  • 1
    That makes perfect sense. adding: fieldLabel[i] = new JLabel(); fileField[i] = new JTextField(); into the FOR loop fixed the problem. Thanks for the speedy response @MadProgrammer – Justiciar Nov 06 '17 at 00:51
  • See [What is a stack trace, and how can I use it to debug my application errors?](http://stackoverflow.com/q/3988788/418556) & [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/q/218384/418556) – Andrew Thompson Nov 06 '17 at 01:53

1 Answers1

0

Thanks to MadProgrammer's comment, this question has been answered.

Editing the loop to:

for(int i = 0; i < fileField.length; i++)
    {
        fieldLabel[i] = new JLabel();
        fileField[i] = new JTextField();
        fieldLabel[i].setText("File "+i+":");  
        fieldPanel.add(fieldLabel[i]);         
        fieldPanel.add(fileField[i]);
    }

resolved the issue.

Justiciar
  • 356
  • 1
  • 3
  • 20