Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: can anyone help me what is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default can anyone help me what is wrong with my code?

    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...


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: can anyone help me what is wrong with my code?

    Welcome to the forum. Please read the Announcement topic to learn how to post code in code or highlight tags and other useful info for newcomers.

  3. #3
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: can anyone help me what is wrong with my code?

    Quote Originally Posted by marc172 View Post
    This the warning what the eclipse compiler always says:

    Exception in thread "main" java.lang.NullPointerException
    First, this is not a compiler "warning" but an exception at runtime. Second, please learn how to interpret the stack trace, it says:

    at colorslider.<init>(colorslider.java:36)

    What is there at line 36? There is this:

    labels.add(redlabel);

    Sice labels was instantiated (line 24) the only possible problem for a NullPointerException is the argument. And if you see, redlabel, greenlabel, bluelabel are declared but not initialized/assigned.
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  4. #4
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can anyone help me what is wrong with my code?

    Quote Originally Posted by andbin View Post
    First, this is not a compiler "warning" but an exception at runtime. Second, please learn how to interpret the stack trace, it says:

    at colorslider.<init>(colorslider.java:36)

    What is there at line 36? There is this:

    labels.add(redlabel);

    Sice labels was instantiated (line 24) the only possible problem for a NullPointerException is the argument. And if you see, redlabel, greenlabel, bluelabel are declared but not initialized/assigned.
    thanks for answering, but it is still not clear to me on how to correct it... Can you tell me how to initialized/assigned those labels?

    and if you don't mind can you please simplify what you have just said... i didn't totally understand it...

    sorry for my ignorance, i am just a newbie in java...

    thanks...

  5. #5
    Member andbin's Avatar
    Join Date
    Dec 2013
    Location
    Italy
    Posts
    443
    Thanks
    4
    Thanked 122 Times in 114 Posts

    Default Re: can anyone help me what is wrong with my code?

    Quote Originally Posted by marc172 View Post
    thanks for answering, but it is still not clear to me on how to correct it... Can you tell me how to initialized/assigned those labels?
    You have declared (as instance variable) a:

    JSlider redSlider
    and you have created an instance of JSlider with
    redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);

    then you have declared a

    JLabel redlabel
    but you have not created an instance of JLabel.
    Do you see in your code something like redlabel = new JLabel("...label..."); ?
    Andrea, www.andbin.netSCJP 5 (91%) – SCWCD 5 (94%)

    Useful links for Java beginnersMy new project Java Examples on Google Code

  6. The Following User Says Thank You to andbin For This Useful Post:

    marc172 (December 22nd, 2013)

  7. #6
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: can anyone help me what is wrong with my code?

    Quote Originally Posted by andbin View Post
    You have declared (as instance variable) a:

    JSlider redSlider
    and you have created an instance of JSlider with
    redSlider = new JSlider(JSlider.HORIZONTAL, 0, 255, 0);

    then you have declared a

    JLabel redlabel
    but you have not created an instance of JLabel.
    Do you see in your code something like redlabel = new JLabel("...label..."); ?


    It works now!! Thank you very much !!!!

Similar Threads

  1. What's wrong with my code?
    By techflyer in forum What's Wrong With My Code?
    Replies: 0
    Last Post: June 13th, 2013, 02:54 PM
  2. What is wrong with my code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 23rd, 2012, 02:40 PM
  3. What's wrong with my code?
    By mjballa in forum What's Wrong With My Code?
    Replies: 8
    Last Post: November 19th, 2011, 03:57 PM
  4. need help .. what is wrong with my code.
    By baig-sh in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 10th, 2011, 07:28 PM
  5. What is wrong with my code???
    By nine05 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 8th, 2011, 09:59 AM