The exception happens at the line
btnNext.addActionListener(new ActionListener() {
What could be null at this line except btnNext
? Nothing. So btnNext
is null.
And indeed, btnNext
is not initialized yet when this line of code is executed, since it's initialized in the constructor of SignInFrame, which is called inside
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
SignInFrame frame = new SignInFrame();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
As its name indicates, what you pass to invokeLater()
will be invoked... later.
Your code, quite frankly, is a mess. Every Swing component should always be accessed from the event dispatch thread. And thus not directly under the main method. That's why the frame is initialized inside the invokeLater
call.
A constructor shouldn't initialize a static field. The buttons should be private instance fields of your frame. And the main method shouldn't deal with them. And having an infinite loop which adds listeners to buttons doesn't make any sense.
Read the Swing tutorial.