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 21 of 21

Thread: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Alright, so I'm getting this exception when running my program (it compiles fine).

    Exception in thread "main" java.lang.NullPointerException
    	at java.awt.Container.addImpl(Container.java:1086)
    	at java.awt.Container.add(Container.java:410)
    	at ColorFactory.<init>(ColorFactory.java:62)
    	at ColorFactory.main(ColorFactory.java:189)

    The first 2, I'm not sure what it's referencing exactly as I dont have a line 410 or 1086.

    Line 62 is commenting.

    Line 189, is the header for my main method.

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ColorFactory extends JFrame
    	{
     
        private final int WINDOW_WIDTH = 500;
        private final int WINDOW_HEIGHT = 300;
        private JPanel topPanel;
        private JPanel bottomPanel;
        private JPanel centerPanel;
        private JLabel labelMessage;
        private JButton red;
        private JButton orange;
        private JButton yellow;
        private ButtonGroup radioButtonGroup;
        private JRadioButton green;
    	 private JRadioButton blue;
        private JRadioButton cyan;
     
     
        /**
          The constructor.
        */
     
    	    public ColorFactory()
    	    {
     
    	        // This is going to call the super constructor.
    	        super("Color Factory");    	  
     
               // Setting the title of the window
               setTitle("Color Factory");
     
    	        // Setting the size of the window based on our constants.
    	        setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
     
    	        // Setting it to exit the program when we close it.
    	        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    	        // Setting our layout
    	        setLayout(new BorderLayout());
     
    	        // These methods build the panels.
    	        buildTopPanel();
    	        buildBottomPanel();
     
               // Creating the center panel.
               centerPanel = new JPanel();
     
    	        // Adding panel
    	        add(centerPanel, BorderLayout.CENTER);
     
    	        // Adding labels
               centerPanel.add(labelMessage);
     
               // The message on the label.
    	        labelMessage = new JLabel("The top buttons will change the anel color and" +
                                "the bottom radio buttons change the text color.");
     
               setVisible(true);
     
    	    }
     
     
    	    private void buildTopPanel()
    	    {
     
    	        topPanel = new JPanel();
    	        add(topPanel, BorderLayout.NORTH);
    	        topPanel.setBackground(Color.WHITE);
     
    	        // Creating the colored buttons.
    	        red = new JButton("Red");
    	        orange = new JButton("Orange");
    	        yellow = new JButton("Yellow");;
     
    	        // Coloring the backgrounds of those buttons.
    	        red.setBackground(Color.RED);
    	        orange.setBackground(Color.ORANGE);
    	        yellow.setBackground(Color.YELLOW);
     
    	        // The action listeners
    	        red.addActionListener(new ButtonListener());
    	        orange.addActionListener(new ButtonListener());
    	        yellow.addActionListener(new ButtonListener());
     
    	        // Adding the buttons
    	        topPanel.add(red);
    	        topPanel.add(orange);
    	        topPanel.add(yellow);
     
    	    }
     
     
    	    private void buildBottomPanel()
    	    {
     
     
    	        bottomPanel=new JPanel();
     
    			  add(bottomPanel, BorderLayout.SOUTH);
     
     
    	        // Setting the background color.
    	        bottomPanel.setBackground(Color.WHITE);
     
    	        // Creating the buttons for this panel.
    	        green=new JRadioButton("Green");
    	        blue=new JRadioButton("Blue");
    	        cyan=new JRadioButton("Cyan");
     
    	        // Setting the foreground color.
    	        green.setForeground(Color.GREEN);
    	        blue.setForeground(Color.BLUE);
    	        cyan.setForeground(Color.CYAN);
     
    	        // Button grouping
               radioButtonGroup=new ButtonGroup();
    	        radioButtonGroup.add(green);
    	        radioButtonGroup.add(blue);
    	        radioButtonGroup.add(cyan);
     
    	        // The action listeners
    	        green.addActionListener(new RadioButtonListener());
    	        blue.addActionListener(new RadioButtonListener());
    	        cyan.addActionListener(new RadioButtonListener());
     
    	        // Adding these buttons
    	        bottomPanel.add(green);
    	        bottomPanel.add(blue);
    	        bottomPanel.add(cyan);
    	    }
     
     
    	    private class ButtonListener implements ActionListener
    	    {
    	        public void actionPerformed(ActionEvent e)
    	        {
     
    	            if(e.getSource() == red)
    	            {
    	              centerPanel.setBackground(Color.RED);
    	            }
     
    	            else if(e.getSource() == orange)
    	            {
    	              centerPanel.setBackground(Color.ORANGE);
    	            }
     
    	            else if(e.getSource() == yellow)
    	            {
    	              centerPanel.setBackground(Color.YELLOW);
    	            }
    	         }
    	    }
     
    	    private class RadioButtonListener implements ActionListener
    	    {
    	        public void actionPerformed(ActionEvent e)
    	        {
    	            if(e.getSource()==green)
    	            {
    	                labelMessage.setForeground(Color.GREEN);
    	            }
     
    	            else if (e.getSource()==blue)
    	            {
    					   labelMessage.setForeground(Color.BLUE);
    	            }
     
    	            else if (e.getSource()==cyan)
    	            {
    	               labelMessage.setForeground(Color.CYAN);
    	            }
    	        }
    	    }
     
     
    	    public static void main(String [] args)
    	    {
    	        new ColorFactory();
    	    }
     
     
    	}

    Maybe I'm misunderstanding the exception, but I can't seem to figure out why this exception is happening.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1086)
    at java.awt.Container.add(Container.java:410)
    at ColorFactory.<init>(ColorFactory.java:62)
    What is null on line 62? Add a println to print out all the variables used on line 62 to see.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    What is null on line 62? Add a println to print out all the variables used on line 62 to see.
    Edit: I realized that for whatever reason, it was showing the wrong line as line 62.

    My real line 62 is:

    centerPanel.add(labelMessage);

    Going to check for the null now..

    Update: I get the exception before I can see the output of the print statements.
    Last edited by Inked.; May 4th, 2014 at 07:41 PM.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    I get the exception before I can see the output
    Put the println BEFORE line 62 so it executes first.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    Put the println BEFORE line 62 so it executes first.
    Haha. Thought I did. Woops.

    They both print null. Both centerPanel and labelMessage.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Ok. They both need to be assigned values before the code tries to use them.
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    Inked. (May 4th, 2014)

  8. #7
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Right. I had realized it was trying to use the labelMessage before I had it created.

    That wasn't the case for the centerPanel one. But once I changed the labelMessage, it still ran without exception.

    Upon running it, it runs and the GUI comes up. I'm having a different exception when I try to use a certain button though. I'm looking into it, but is it preferred that I make a new thread that's more descriptive to the new exception?

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    If it's the same program, post it here.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    	at ColorFactory$ButtonListener.actionPerformed(ColorFactory.java:155)
    	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    	at java.awt.Component.processMouseEvent(Component.java:6505)
    	at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
    	at java.awt.Component.processEvent(Component.java:6270)
    	at java.awt.Container.processEvent(Container.java:2229)
    	at java.awt.Component.dispatchEventImpl(Component.java:4861)
    	at java.awt.Container.dispatchEventImpl(Container.java:2287)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    	at java.awt.Container.dispatchEventImpl(Container.java:2273)
    	at java.awt.Window.dispatchEventImpl(Window.java:2719)
    	at java.awt.Component.dispatchEvent(Component.java:4687)
    	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
    	at java.awt.EventQueue.access$200(EventQueue.java:103)
    	at java.awt.EventQueue$3.run(EventQueue.java:694)
    	at java.awt.EventQueue$3.run(EventQueue.java:692)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    	at java.awt.EventQueue$4.run(EventQueue.java:708)
    	at java.awt.EventQueue$4.run(EventQueue.java:706)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    	at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
    	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    	at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

    The lines vary depending on which button it is.

    Essentially, the buttons in the top panel are suppose to set the background color. While the buttons in the bottom panel will set the text color. The bottom panel works flawlessly. But pressing any button in the top panel creates this large of exceptions and makes no changes.

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at ColorFactory$ButtonListener.actionPerformed(ColorF actory.java:155)
    Look at line 155 and find what's null.
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    Look at line 155 and find what's null.
    Line 155 is:

    centerPanel.setBackground(Color.ORANGE);

    centerPanel is definitely created before it's used. The only time it's even mentioned before creation is up in the definitions. With a print of it (placed after it's created) being:

    javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

    The syntax is correct for the rest, it's using the setBackground and colors.

  13. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    What is null on line 155 when that line is executed? You can't always tell just by looking. You need to print out the values of all the variables used on that line to see which one is null.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #13
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Just a quick suggestion:
    It really helps if you declare your GUI components as "final" variables. A variable that is final can only be assigned once. This way you can never overwrite it by accident and you can be sure it is not null as long as you create it in the constructor. (the drawback is you HAVE to assign it a value in the constructor, oh well.)
    Maybe this will help you.

  15. #14
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    What is null on line 155 when that line is executed? You can't always tell just by looking. You need to print out the values of all the variables used on that line to see which one is null.
    Well centerPanel is the only variable on the line. The code I posted was the line in full.

    The only variable on that line is centerPanel, and when printed I get:

    javax.swing.JPanel[,0,0,0x0,invalid,layout=java.awt.FlowLayout,alignmentX=0.0,alignmentY=0.0,border=,flags=9,maximumSize=,minimumSize=,preferredSize=]

    This is line 155 exactly as it is:

    	              centerPanel.setBackground(Color.RED);

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    A non null value will not cause a NullPointerException. Are you looking at the correct line?
    Did you print out the value of the variable immediately before line 155?
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    A non null value will not cause a NullPointerException. Are you looking at the correct line?
    Did you print out the value of the variable immediately before line 155?
    I'm definitely looking at the correct line.

    My print statement is up at line 62, right after the panel is created.

    Edit: Inside my control strucutre right before line 155, it prints null. But I'm not sure why or how it got that way.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    My print statement is up at line 62, right after the panel is created.
    It needs to be next to where the variable is used.
    right before line 155, it prints null
    Is there more than one variable with the same name? One with a value and one that is null?
    If you don't understand my answer, don't ignore it, ask a question.

  19. #18
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Quote Originally Posted by Norm View Post
    It needs to be next to where the variable is used.

    Is there more than one variable with the same name? One with a value and one that is null?
    Negative. It's defined, then it creates the panel and sets it center. It adds a label message and then later it's used in a control structure to set the background color depending on what button the user pressed (which isn't working).

    The code after my recent updates, I can't find an instance where it has been duplicated or nullified:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    public class ColorFactory extends JFrame
    	{
     
        private final int WINDOW_WIDTH = 500;
        private final int WINDOW_HEIGHT = 300;
        private JPanel topPanel;
        private JPanel bottomPanel;
        private JPanel centerPanel;
        private JLabel labelMessage;
        private JButton red;
        private JButton orange;
        private JButton yellow;
        private ButtonGroup radioButtonGroup;
        private JRadioButton green;
    	 private JRadioButton blue;
        private JRadioButton cyan;
     
     
        /**
          The constructor.
        */
     
    	    public ColorFactory()
    	    {
     
     
    	        // This is going to call the super constructor.
    	        super("Color Factory");
     
     
     
     
               // Setting the title of the window
               setTitle("Color Factory");
     
    	        // Setting the size of the window based on our constants.
    	        setSize (WINDOW_WIDTH, WINDOW_HEIGHT);
     
    	        // Setting it to exit the program when we close it.
    	        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    	        // Setting our layout
    	        setLayout(new BorderLayout());
     
    	        // These methods build the panels.
    	        buildTopPanel();
    	        buildBottomPanel();
     
               // Creating the center panel.
               JPanel centerPanel = new JPanel();
     
     
     
     
    	        // Adding the centerl panel
    	        add(centerPanel, BorderLayout.CENTER);
     
               // The message on the label.
    	        labelMessage = new JLabel("The top buttons will change the panel color and" +
                                "the bottom radio buttons change the text color.");
     
    	        // Adding the label message.
               centerPanel.add(labelMessage);
     
               setVisible(true);
     
    	    }
     
     
    	    private void buildTopPanel()
    	    {
     
    	        JPanel topPanel = new JPanel();
    	        add(topPanel, BorderLayout.NORTH);
    	        topPanel.setBackground(Color.WHITE);
     
    	        // Creating the colored buttons.
    	        red = new JButton("Red");
    	        orange = new JButton("Orange");
    	        yellow = new JButton("Yellow");;
     
    	        // Coloring the backgrounds of those buttons.
    	        red.setBackground(Color.RED);
    	        orange.setBackground(Color.ORANGE);
    	        yellow.setBackground(Color.YELLOW);
     
    	        // The action listeners
    	        red.addActionListener(new ButtonListener());
    	        orange.addActionListener(new ButtonListener());
    	        yellow.addActionListener(new ButtonListener());
     
    	        // Adding the buttons
    	        topPanel.add(red);
    	        topPanel.add(orange);
    	        topPanel.add(yellow);
     
    	    }
     
     
    	    private void buildBottomPanel()
    	    {
     
               JPanel bottomPanel = new JPanel();
    			  add(bottomPanel, BorderLayout.SOUTH);
     
     
    	        // Setting the background color.
    	        bottomPanel.setBackground(Color.WHITE);
     
    	        // Creating the buttons for this panel.
    	        green = new JRadioButton("Green");
    	        blue = new JRadioButton("Blue");
    	        cyan = new JRadioButton("Cyan");
     
    	        // Setting the foreground color.
    	        green.setForeground(Color.GREEN);
    	        blue.setForeground(Color.BLUE);
    	        cyan.setForeground(Color.CYAN);
     
    	        // Button grouping
               radioButtonGroup=new ButtonGroup();
    	        radioButtonGroup.add(green);
    	        radioButtonGroup.add(blue);
    	        radioButtonGroup.add(cyan);
     
    	        // The action listeners
    	        green.addActionListener(new RadioButtonListener());
    	        blue.addActionListener(new RadioButtonListener());
    	        cyan.addActionListener(new RadioButtonListener());
     
    	        // Adding these buttons
    	        bottomPanel.add(green);
    	        bottomPanel.add(blue);
    	        bottomPanel.add(cyan);
    	    }
     
     
    	    private class ButtonListener implements ActionListener
    	    {
    	        public void actionPerformed(ActionEvent e)
    	        {
     
    	            if(e.getSource() == red)
    	            {
                      System.out.println(centerPanel);
    	              centerPanel.setBackground(Color.RED);
    	            }
     
    	            else if(e.getSource() == orange)
    	            {
    	              centerPanel.setBackground(Color.ORANGE);
    	            }
     
    	            else if(e.getSource() == yellow)
    	            {
    	              centerPanel.setBackground(Color.YELLOW);
    	            }
    	         }
    	    }
     
    	    private class RadioButtonListener implements ActionListener
    	    {
    	        public void actionPerformed(ActionEvent e)
    	        {
    	            if(e.getSource() == green)
    	            {
    	                labelMessage.setForeground(Color.GREEN);
    	            }
     
    	            else if (e.getSource() == blue)
    	            {
    					   labelMessage.setForeground(Color.BLUE);
    	            }
     
    	            else if (e.getSource() == cyan)
    	            {
    	               labelMessage.setForeground(Color.CYAN);
    	            }
    	        }
    	    }
     
     
    	    public static void main(String [] args)
    	    {
    	        new ColorFactory();
    	    }
     
     
    	}

  20. #19
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    There are two definitions:
        private JPanel centerPanel;   //<<<<<<<<<This one is never given a value.
     
     
           // Creating the center panel.
               JPanel centerPanel = new JPanel();   //  Local to main() method only

    The code in post#1 did it the right way.
    If you don't understand my answer, don't ignore it, ask a question.

  21. The Following User Says Thank You to Norm For This Useful Post:

    Inked. (May 5th, 2014)

  22. #20
    Junior Member
    Join Date
    May 2014
    Posts
    22
    My Mood
    Tired
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    That works, but it's weird because I thought I had to have the
    JPanel centerPanel = new JPanel();

    For all of them? Because the top and bottom panels are like that and they work aswell. Like, my top panel has:

    JPanel topPanel = new JPanel();

    And is also defined up top aswell, but it still works?

  23. #21
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Exception in thread "main" java.lang.NullPointerException || Cannot find the problem?

    Do some research on the "scope" of a definition for a variable.
    As long as you don't try to use topPanel outside of the method, there won't be a problem. The topPanel variable defined at the class level will be null.
    The one in the method will have a value.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. "Exception in thread "main" java.lang.NullPointerException" help?
    By redstripes in forum What's Wrong With My Code?
    Replies: 4
    Last Post: February 28th, 2014, 08:59 AM
  2. I get the error Exception in thread "main" java.lang.NullPointerException?
    By jeremy28 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: October 3rd, 2013, 11:13 PM
  3. Replies: 1
    Last Post: April 7th, 2013, 03:40 PM
  4. Exception in thread "main" java.lang.NullPointerException problem.......
    By Adam802 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 20th, 2012, 02:23 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM