-2
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.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;


public class JFrameTest  {

    private MyListener listener = new MyListener();
    private JButton clear = null;
   private JButton addOne = null;
   private static JFrame frame = null;
   private JTextField text1 = null;
   private JTextField text2 = null;
   private JTextField text3 = null;
   private JTextField text4 = null;
   private JTextField text5 = null;
   private JTextField text6 = null;

public static void main(String[] args) {

    javax.swing.SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            createAndShowGUI();
        }
    });
}
private static void createAndShowGUI() {
    //Create and set up the window.
    frame = new JFrame("Button & Listener Demo");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(new JFrameTest().createGUI());

    //Display the window.
    frame.setSize(800, 500);
    frame.setVisible(true);
}

public JTabbedPane createGUI(){
    JTabbedPane tabbedPane = new JTabbedPane();
    JPanel one = new JPanel();
    one.setLayout(new BorderLayout());
    JPanel test = new JPanel();
    JPanel test2 = new JPanel();


   JLabel label1 = new JLabel ("Pound");
   JLabel label2 = new JLabel("Ounce :" );
   JLabel label3 = new JLabel("Ton :");
   JLabel label4 = new JLabel("Tonne :");
   JLabel label5 = new JLabel("Kilogram :");
   JLabel label6 = new JLabel("Gram :");
   text1 = new JTextField(5);
   text2 = new JTextField(5);
   text3 = new JTextField(5);
   text4 = new JTextField(5);
   text5 = new JTextField(5);
   text6 = new JTextField(5);
   JButton button = new JButton();

    clear = new JButton("Clear");
    clear.addActionListener(listener);

    text1.setText("0");
    text2.setText("0");
    text3.setText("0");
    text4.setText("0");
    text5.setText("0");
    text6.setText("0");
    test.add(label1);
    test.add(text1);
    test.add(label2);
    test.add(text2);
    test.add(label3);
    test.add(text3);
    test2.add(label4);
    test2.add(text4);
    test2.add(label5);
    test2.add(text5);
    test2.add(label6);
    test2.add(text6);
    one.add(test, BorderLayout.NORTH);
    one.add(test2, BorderLayout.CENTER);
    one.add(clear, BorderLayout.SOUTH);

    tabbedPane.addTab("Tab 1", one);
    JPanel two = new JPanel();
    tabbedPane.addTab("Tab 2", two);




    return tabbedPane;
    }




public class MyListener implements ActionListener{

    @Override
    public void actionPerformed(ActionEvent e) {
        JButton source = (JButton)e.getSource();
                    String sourceName = source.getName();

                    if(sourceName.equals("Clear"))
                    {
                        System.out.println("yeet");
                        text1.setText("00");                           
                        text2.setText("00");
                      text3.setText("00");
                       text4.setText("00");
                       text5.setText("00");
                      text6.setText("00");
                    }
                    else
                    {
                        System.out.println("nah");
                    }


    }

}


}

And my error code is:Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at jframetest.JFrameTest$MyListener.actionPerformed(JFrameTest.java:123) at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022) at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348) at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402) at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259) at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252) at java.awt.Component.processMouseEvent(Component.java:6533) at javax.swing.JComponent.processMouseEvent(JComponent.java:3324) at java.awt.Component.processEvent(Component.java:6298) at java.awt.Container.processEvent(Container.java:2236) at java.awt.Component.dispatchEventImpl(Component.java:4889) at java.awt.Container.dispatchEventImpl(Container.java:2294) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888) at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525) at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466) at java.awt.Container.dispatchEventImpl(Container.java:2280) at java.awt.Window.dispatchEventImpl(Window.java:2746) at java.awt.Component.dispatchEvent(Component.java:4711) at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758) at java.awt.EventQueue.access$500(EventQueue.java:97) at java.awt.EventQueue$3.run(EventQueue.java:709) at java.awt.EventQueue$3.run(EventQueue.java:703) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90) at java.awt.EventQueue$4.run(EventQueue.java:731) at java.awt.EventQueue$4.run(EventQueue.java:729) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80) at java.awt.EventQueue.dispatchEvent(EventQueue.java:728) at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201) at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116) at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101) at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93) at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

thej1996
  • 1
  • 1

2 Answers2

1

The only two usages of the JButton are basically

clear = new JButton("Clear");
clear.addActionListener(listener);

So, you aren't setting a name for the Component.
Here sourceName is null.

String sourceName = source.getName();

And here is your NullPointerException.

sourceName.equals("Clear")

A general tip, when comparing Strings, use always the one which is never null to perform the operation, in this case

if ("Clear".equals(sourceName)) { ... }

The Component#getName method (inherited) may seem to construct a name for you, if it is null

if (name == null && !nameExplicitlySet) {
    synchronized(getObjectLock()) {
        if (name == null && !nameExplicitlySet)
            name = constructComponentName();
    }
}
return name;

But, constructComponentName returns null

String constructComponentName() {
    return null; // For strict compliance with prior platform versions, a Component
                 // that doesn't set its name should return null from
                 // getName()
}
LppEdd
  • 20,274
  • 11
  • 84
  • 139
0

It is because you are not setting the .name property of the button, just the text property. The default constructor set the text property not the name.

You can set the name property of the button.

    clear = new JButton("Clear");//this just set the text property
    clear.setName("Clear"); //here

Or just change your if condition like

String sourceName = source.getText(); //chage .getName for .getText
if(sourceName.equals("Clear")) //now will work
dgofactory
  • 348
  • 5
  • 13