Hello, i am just a beginner in java programming, can anyone help me how to figure out what is wrong with my codes??
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
public class colorslider extends JFrame{
private JSlider redSlider, greenSlider, blueSlider;
private JLabel redlabel, greenlabel, bluelabel;
private JPanel colorpanel, sliders, labels;
public colorslider(){
redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
colorpanel = new JPanel();
colorpanel.setBackground(Color.BLACK);
Container pane = this.getContentPane();
pane.setLayout(new GridLayout(1, 3, 3, 3));
sliders = new JPanel();
labels = new JPanel();
pane.add(sliders);
pane.add(labels);
pane.add(colorpanel);
sliders.setLayout(new GridLayout(3, 1, 2, 2));
sliders.add(redSlider);
sliders.add(greenSlider);
sliders.add(blueSlider);
labels.setLayout(new GridLayout(3, 1, 2, 2));
labels.add(redlabel);
labels.add(greenlabel);
labels.add(bluelabel);
event e = new event();
redSlider.addChangeListener(e);
greenSlider.addChangeListener(e);
blueSlider.addChangeListener(e);
}
public class event implements ChangeListener{
public void stateChanged(ChangeEvent e){
int r = redSlider.getValue();
int g = greenSlider.getValue();
int b = blueSlider.getValue();
redlabel.setText("Red = " + r);
greenlabel.setText("Greeb = " + g);
bluelabel.setText("Blue = " + b);
colorpanel.setBackground(new Color(r, g, b));
}
}
public static void main(String[] args){
colorslider cs = new colorslider();
cs.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cs.setTitle("Color Slider");
cs.setVisible(true);
cs.setSize(300, 110);
}
}
This the warning what the eclipse compiler always says:
Exception in thread "main" java.lang.NullPointerException
at java.awt.Container.addImpl(Unknown Source)
at java.awt.Container.add(Unknown Source)
at colorslider.<init>(colorslider.java:36)
at colorslider.main(colorslider.java:64
Please help me!
Thank You...