Purpose of the code : Create two Buttons(button1 and button2). When User clicks button1, change the text of button2. When User clicks button2, change the text of button1.
Here's the code I'm using :
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class multiAL {
JButton button1;
JButton button2;
JFrame frame;
public static void main(String[] args) {
multiAL setterAL = new multiAL();
setterAL.go();
}
public void go() {
button1 = new JButton("Click me, I'm One");
button2 = new JButton("Click me, I'm Two");
frame.setSize(500,500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(BorderLayout.WEST, button1);
frame.getContentPane().add(BorderLayout.EAST, button2);
frame.setVisible(true);
button1.addActionListener(new b1L());
button2.addActionListener(new b2L());
}
class b1L implements ActionListener {
public void actionPerformed(ActionEvent event) {
button2.setText("What??, you clicked 1??");
}
}
class b2L implements ActionListener {
public void actionPerformed(ActionEvent event) {
button1.setText("What??, you clicked 2??");
}
}
}
It compiles perfectly, but when I run it I receive following error :
Exception in thread "main" java.lang.NullPointerException
at multiAL.go(multiAL.java:17)
at multiAL.main(multiAL.java:11)
Till now, I've encountered only compile-time errors. So there are two question which I want to ask:
1) What's wrong with the code? 2) How to track down runtime errors?