Here is my code
import javax.swing.JFrame;
public class Server
{
//-----------------------------------------------------------------
// Creates and display the main program frame.
//-----------------------------------------------------------------
public static void main (String[] args)
{
JFrame frame = new JFrame ("exit");
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
ServerPanel panel = new ServerPanel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
And its panel
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ServerPanel extends JPanel
{
private JButton push;
public ServerPanel ()
{
push = new JButton ("Click this button to close the server.....");
push.addActionListener (new ButtonListener());
add (push);
setBackground (Color.red);
setPreferredSize (new Dimension(500, 500));
}
private class ButtonListener implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
System.exit(0);
}
}
}
I just want to create a simple GUI that will show a button that if its clicked, it will immediately close the program ,but every time I try to run the Server.java it gives me this:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at ServerPanel.<init>(ServerPanel.java:20)
at Server.main(Server.java:12)
Can anybody fix it?