Adding textfield and label on mouse click not working Java

475 views Asked by At

I have this code:

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

public class Interface2 {

    private JLabel label;
    private JLabel label2;
    private JLabel label3;
    private JTextField textfield;
    private JTextField textfield2;
    private JTextField textfield3;
    private JButton button;
    private JButton button2;
    private JButton button3;
    private JPanel panel;

    public static void main(String[] args) {
        new Interface();
    }

    public Interface2() {
        JFrame frame = new JFrame("Test");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 600);

        panel = new JPanel();

        JPanel buttonPane = new JPanel();
        label = new JLabel("Button 1");
        label2 = new JLabel("Button 2");
        label3 = new JLabel("Button 3");

        textfield = new JTextField(5);

        JButton button = new JButton("cirkel");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label);
                panel.add(textfield);
                panel.revalidate();
                panel.repaint();
                String text = textfield.getText();

                // Van de String 'text' een double maken

//      cirkel C1 = new cirkel();
//      C1.setDiam(diameter);
                label.setText("De diameter = " + 1 + " cm \n\n");
                label.setText("De straal = " + 2 + " cm");
                label.setText("De oppervlakte van de cirkel = " + 3 + " cm2");
            }
        });

        JButton button2 = new JButton("driehoek");
        button2.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label2);
                panel.add(textfield2);
                panel.revalidate();
                panel.repaint();
                // driehoek
            }
        });

        JButton button3 = new JButton("vierhoek");
        button3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                panel.removeAll();
                panel.add(label3);
                panel.add(textfield3);
                panel.revalidate();
                panel.repaint();
                // vierhoek
            }
        });

        buttonPane.add(button);
        buttonPane.add(button2);
        buttonPane.add(button3);

        frame.add(buttonPane, BorderLayout.NORTH);
        frame.add(panel);
        frame.setVisible(true);
    }
}

I want to add a label and a textfield for each of the three buttons.

But if I click on any button in this case it gives me an error.

I don't understand why it's not working and I'm new to Java...

This is the error:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at Interface.actionPerformed(Interface.java:68)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
    at java.awt.Component.processMouseEvent(Unknown Source)
    at javax.swing.JComponent.processMouseEvent(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.LightweightDispatcher.retargetMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
    at java.awt.LightweightDispatcher.dispatchEvent(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$500(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)
2

There are 2 answers

0
Andrew Thompson On BEST ANSWER
public static void main(String[] args) {
    new Interface();
}

public Interface2() {
    // ..

It is likely that the main method of the Interface2 class should not be creating an instance of Interface!

0
Stefan Freitag On

Please ensure that textfield2 and textfield3 are initialized.

textfield2 = new JTextField(5);
textfield3 = new JTextField(5);

Otherwise you will receive a NullPointerException.