Error Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException in Java program

In your Java code, you have the following code segment:
public class BigOne {
public static int i = 0;
    JButton onebutton;
    JLabel label;
    JFrame frame;

   public static void main(String[] args) {
      BigOne gui = new BigOne();
      gui.go();
    }

public void go() {
    frame = new JFrame();
    frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
    JButton onebutton = new JButton(“Click Here”);
    onebutton.addActionListener(new BListener());

   JLabel label = new JLabel(“This is dummy text”);

When you compile the code, you receive no errors. However, at runtime, you get an error as follows:
Exception in thread “AWT-EventQueue-0” java.lang.NullPointerException

This is because you declared the JButton and JLabel variables at the start of the class, and then re-declared them in the go() function.

If you:
remove JButton from JButton onebutton = new JButton(“Click Here”), and;
remove JLabel from JLabel label = new JLabel(“This is dummy text”);

and re-compile, you will find that the runtime error does not occur.