I'm trying to draw a traffic light. Basically a long rectangle shaped frame with 4 buttons attached to it. I've followed the instructions I've been given but I must be missing something. When I run the program I get:
Exception in thread "main" java.lang.NullPointerException
at TrafficLightPanel.<init>(TrafficLightPanel.java:14)
at TrafficLightApp.main(TrafficLightApp.java:11)
Any help on this would be great ^^
Main method
import javax.swing.*;
public class TrafficLightApp {
public static void main(String[] args) {
JFrame frame = new JFrame("Traffic Lights");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
TrafficLightPanel lightPanel = new TrafficLightPanel(); //line 11
frame.add(lightPanel);//JFrame containing instance of TrafficLightPanel class
}
}
Support class
import javax.swing.*;
import java.awt.*;
public class TrafficLightPanel extends JPanel{
private JButton red,amber,green,change;
private JLabel buttonLabel,lastPressed;
private JPanel buttonPanel;
public TrafficLightPanel(){
//JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setSize(200,400);
panel.setBackground(Color.blue);
buttonPanel.setSize(80,390); //line 14
buttonPanel.setBackground(Color.white);
buttonPanel.add(red);
buttonPanel.add(amber);
buttonPanel.add(green);
buttonPanel.add(change);
buttonPanel.add(buttonLabel);
panel.add(buttonPanel);
}
}