#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2

    Can't figure out why it compiles, but won't run


    I'm writing this GUI program which contains three buttons, red, yellow and green. I want the image to update on which ever button I press. I don't get any errors when I compiles, but it says

    Code:
    Exception in thread "main" java.lang.NullPointerException
    	at trafficlight.trafficLightPanel.<init>(trafficLightPanel.java:22)
    	at trafficlight.TrafficLight.main(TrafficLight.java:11)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)
    Code:
    package trafficlight;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class trafficLightPanel extends JPanel{
        
        private JPanel buttonPanel, lightPanel;
        private JButton red, yellow, green;
        private JLabel imageLabel;
        
        public trafficLightPanel()
        {
            red = new JButton("Red");
            red.addActionListener(new buttonListener());
            yellow = new JButton("Yellow");
            red.addActionListener(new buttonListener());
            green = new JButton("Green");
            red.addActionListener(new buttonListener());
            
            buttonPanel.add(red);
            buttonPanel.add(yellow);
            buttonPanel.add(green);
            setPreferredSize(new Dimension(150,300));
            setBackground(Color.black);
            
            lightPanel.add(imageLabel);
            setPreferredSize(new Dimension(200,350));
            setBackground(Color.cyan);          
        }
        
        private class buttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if(event.getSource()==red)
                    imageLabel = new JLabel(new ImageIcon("red.png"));
                else if (event.getSource()==yellow)
                    imageLabel = new JLabel(new ImageIcon("yellow.png"));
                else if (event.getSource()==green)
                    imageLabel = new JLabel(new ImageIcon("green.png"));
                
            }
        }
        
    }
  2. #2
  3. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Note you never initialize buttonPanel
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    Note you never initialize buttonPanel
    Now, I've initialized buttonPanel and lightPanel, but still showing same message.

    Code:
    package trafficlight;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class trafficLightPanel extends JPanel{
        
        private JPanel buttonPanel, lightPanel;
        private JButton red, yellow, green;
        private JLabel imageLabel;
        
        public trafficLightPanel()
        {
            red = new JButton("Red");
            red.addActionListener(new buttonListener());
            yellow = new JButton("Yellow");
            red.addActionListener(new buttonListener());
            green = new JButton("Green");
            red.addActionListener(new buttonListener());
            
            buttonPanel = new JPanel();
            buttonPanel.add(red);
            buttonPanel.add(yellow);
            buttonPanel.add(green);
            setPreferredSize(new Dimension(150,300));
            setBackground(Color.black);
            
            lightPanel = new JPanel();
            lightPanel.add(imageLabel);
            setPreferredSize(new Dimension(200,350));
            setBackground(Color.cyan);          
        }
        
        private class buttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if(event.getSource()==red)
                    imageLabel = new JLabel(new ImageIcon("red.png"));
                else if (event.getSource()==yellow)
                    imageLabel = new JLabel(new ImageIcon("yellow.png"));
                else if (event.getSource()==green)
                    imageLabel = new JLabel(new ImageIcon("green.png"));
                
            }
        }
        
    }
  6. #4
  7. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    On what line is the error?
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    On what line is the error?
    None, there's no red squiggly lines. It says build successful, but nothing is displayed.
  10. #6
  11. Java Junkie
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jan 2004
    Location
    Mobile, Alabama
    Posts
    4,022
    Rep Power
    1285
    Originally Posted by tonynsx
    None, there's no red squiggly lines. It says build successful, but nothing is displayed.
    I'm not sure what you're trying to do, but note that imageLabel is null when it is added to lightPanel
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    78
    Rep Power
    2
    Originally Posted by bullet
    I'm not sure what you're trying to do, but note that imageLabel is null when it is added to lightPanel
    Thanks for your help, it works finally.

    Code:
    package trafficlight;
    
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.image.*;
    
    public class trafficLightPanel extends JPanel{
        
        private JPanel buttonPanel, lightPanel;
        private JButton red, yellow, green;
        private JLabel imageLabel;
        
        public trafficLightPanel()
        {
            red = new JButton(new ImageIcon("redBall.png"));
            red.addActionListener(new buttonListener());
            yellow = new JButton(new ImageIcon("yellowBall.png"));
            yellow.addActionListener(new buttonListener());
            green = new JButton(new ImageIcon("greenBall.png"));
            green.addActionListener(new buttonListener());
            
            buttonPanel = new JPanel();
            buttonPanel.add(red);
            buttonPanel.add(yellow);
            buttonPanel.add(green);
            buttonPanel.setPreferredSize(new Dimension(100,250));
            buttonPanel.setBackground(Color.black);
            
            imageLabel = new JLabel();
            lightPanel = new JPanel();
            lightPanel.add(imageLabel);
            lightPanel.setPreferredSize(new Dimension(500,850));
            lightPanel.setBackground(Color.white);       
            
            add(buttonPanel);
            add(lightPanel);
            
            setBackground(Color.black);
        }
        
        private class buttonListener implements ActionListener
        {
            public void actionPerformed(ActionEvent event)
            {
                if(event.getSource()==green)
                    imageLabel.setIcon(new ImageIcon("green.jpg"));
                else if (event.getSource()==yellow)
                    imageLabel.setIcon(new ImageIcon("yellow.jpg"));
                else if (event.getSource()==red)
                    imageLabel.setIcon(new ImageIcon("red.jpg"));           
            }
        }
        
    }

IMN logo majestic logo threadwatch logo seochat tools logo