javaswinguser-interfaceruntime-errorjslider

Runtime error when implementing Java gui sliders


My first time posting a question here. Been coming here a while and enjoyed reading the threads. Was hoping someone on here could help me with a program I've been doing to learn Java. The program calls to implement sliders to change the background color in a gui background. It compiles fine, but when I run it, I get a few errors, which I commented in at the end of the code.

Code is as follows:

import java.awt.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.*;


public class sliderDemo extends JFrame
{
private JSlider redSlider, greenSlider, blueSlider;
private JPanel labels, sliders, colors;
private JLabel redlabel,greenlabel, bluelabel, colorlabel; 
JTextArea colorPanel;

public sliderDemo()
{
setTitle("Slider Excercise");
setLayout(new BorderLayout(5, 5));

ChangeListener event = new eventListener();

colorlabel = new JLabel("Sliders to change colors:");
redlabel = new JLabel("Red slider");
greenlabel = new JLabel("Green slider");
bluelabel = new JLabel("Blue slider");

labels = new JPanel();
labels.setLayout(new GridLayout(3,1));
labels.add(redlabel);
labels.add(greenlabel);
labels.add(bluelabel);

redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
redSlider.addChangeListener(event);
redSlider.setMaximum(255);
redSlider.setPaintLabels(true);
redSlider.setPaintTicks(true);
redSlider.setMajorTickSpacing(25);
redSlider.setMinorTickSpacing(5);
redSlider.setPaintTrack(false);

greenSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
greenSlider.addChangeListener(event);
greenSlider.setMaximum(255);
greenSlider.setPaintLabels(true);
greenSlider.setPaintTicks(true);
greenSlider.setMajorTickSpacing(25);
greenSlider.setMinorTickSpacing(5);
greenSlider.setPaintTrack(false);

blueSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);
blueSlider.addChangeListener(event);
blueSlider.setMaximum(255);
blueSlider.setPaintLabels(true);
blueSlider.setPaintTicks(true);
blueSlider.setMajorTickSpacing(25);
blueSlider.setMinorTickSpacing(5);
blueSlider.setPaintTrack(false);

sliders = new JPanel();
sliders.setLayout(new GridLayout(3,1)); 
sliders.add(colorlabel);
sliders.add(redSlider);
sliders.add(greenSlider);
sliders.add(blueSlider);

colorPanel = new JTextArea(10, 10);
colorPanel.setEditable(false);
colorPanel.setBackground(Color.WHITE);

colorPanel.add(sliders, BorderLayout.CENTER);
colorPanel.add(colors, BorderLayout.NORTH);
colorPanel.add(labels, BorderLayout.WEST);

colors = new JPanel(new BorderLayout(5, 5));
colors.add(colorlabel);
colors.add(colorPanel, BorderLayout.NORTH);
}

public static void main(String[] args)
{
JFrame myFrame = new sliderDemo();
myFrame.setSize(500, 500);
myFrame.setVisible(true);
myFrame.setLocationRelativeTo(null);
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

public class eventListener implements ChangeListener
{
    public void stateChanged(ChangeEvent e)
    {
    int r = redSlider.getValue();
    int g = greenSlider.getValue();
    int b = blueSlider.getValue();
    colorPanel.setBackground(new Color(r, g, b));
    }
}
}

/*
Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1090)
    at java.awt.Container.add(Container.java:966)
    at sliderDemo.<init>(sliderDemo.java:79)
    at sliderDemo.main(sliderDemo.java:89)
Press any key to continue . . .

Solution

    1. add JComponents that are initialized

    2. you tried to add JPanel colors to JTextArea (quite nonsence) and its intialization colors = new JPanel(new BorderLayout(5, 5)); is in the next code lines

    3. rename JTextArea colorPanel; to JTextArea textArea

    4. then you miss 4th JPanel, because JTextArea colorPanel is called textArea and JTextArea isn't container for JPanels, is designated for user keys input