0

I have a class that extends JDialog to create a popup with several textboxes for the user to enter data into. When the "OK" button is clicked the data then gets stored into an ArrayList. If no data is entered into the textboxes and the user clicks "OK" everything works fine. I can still access the ArrayList.

The problem I am running into is when the Close button of the JDialog is clicked. No matter what happens to the textboxes (data or no data) I receive a NullPointerException when I try to access the ArrayList. Even when I try to test for null.

What is the proper way to handle this? And yes I am fairly new to Java. Perhaps my approach is wrong from the start?

My main program calls the getData() method to retrieve the data. I am thinking this may be wrong, but it seems to work when the "OK" button is clicked.

*edit to add code

public class DataPopup extends JDialog{
    ArrayList<String> data;
    ArrayList<JTextField> dataFields = new ArrayList<JTextField>();

    public DataPopup(Frame parent) {
        super(parent);
        initUI();
    }

    private void initUI() {

        JLabel name = new JLabel("Enter Data");
        name.setFont(new Font("Arial", Font.BOLD, 13));

        JLabel custNameLabel = new JLabel("Customer Name");
        JTextField custNameField = new JTextField();
        dataFields.add(custNameField);

        JLabel serialNumLabel = new JLabel("Serial Number");
        JTextField serialNumField = new JTextField();
        dataFields.add(serialNumField);

        JLabel notesLabel = new JLabel("Notes");
        JTextField notesField = new JTextField();
        dataFields.add(notesField);

        JButton btn = new JButton("OK");
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed (ActionEvent event) {
                data = new ArrayList<String>();

                for(int i = 0; i < dataFields.size(); i++) {
                    data.add(dataFields.get(i).getText());
                }

                dispose();
            }
        });

        createLayout(name, 
                    custNameLabel, 
                    custNameField,  
                    serialNumLabel, 
                    serialNumField, 
                    notesLabel, 
                    notesField, 
                    btn);

        setModalityType(ModalityType.APPLICATION_MODAL);

        setTitle("Information");
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setLocationRelativeTo(getParent());
    }


    public ArrayList<String> getData() {
        return data;
    }

    private void createLayout(JComponent... arg) {

        Container pane = getContentPane();
        GroupLayout g1 = new GroupLayout(pane);
        pane.setLayout(g1);

        g1.setAutoCreateContainerGaps(true);
        g1.setAutoCreateGaps(true);

        g1.setHorizontalGroup(g1.createParallelGroup(CENTER)
                .addComponent(arg[0])
                .addComponent(arg[1])
                .addComponent(arg[2])
                .addComponent(arg[3])
                .addComponent(arg[4])
                .addComponent(arg[5])
                .addComponent(arg[6])
                .addComponent(arg[7])
                .addGap(200)
        );

        g1.setVerticalGroup(g1.createSequentialGroup()
                .addGap(30)
                .addComponent(arg[0])
                .addGap(20)
                .addComponent(arg[1])
                .addGap(10)
                .addComponent(arg[2])
                .addGap(20)
                .addComponent(arg[3])
                .addGap(10)
                .addComponent(arg[4])
                .addGap(20)
                .addComponent(arg[5])
                .addGap(10)
                .addComponent(arg[6])
                .addGap(20)
                .addComponent(arg[7])//OK button
                .addGap(30)
        );

        pack();
    }
}
Daedlin
  • 1
  • 2
  • You probably should provide some example code. – Christian Frommeyer Feb 10 '17 at 19:19
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –  Feb 10 '17 at 19:25
  • I am guessing everyone is agreeing with Will on this one. I understand something like having a class with some method in it, then creating a reference to the class and calling the method through that reference will cause a NPE. I don't understand how clicking the "OK" button on the JDialog works but clicking the "Close" button causes the NPE. Can anyone elaborate a bit? Point me in the right direction? – Daedlin Feb 13 '17 at 13:17
  • Wow, guess not. – Daedlin Feb 20 '17 at 13:26

0 Answers0