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

Thread: GUI won't run properly once panel is added

  1. #1
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default GUI won't run properly once panel is added

    Hi,
    This GUI will compile but does not run. I know it is something to do with panel 'p2' as when I remove it, it runs fine.
    I have posted the error below. Help would be greatly appreciated.
    Thanks.

    Error: Exception in thread "main" java.lang.NullPointerException
    at TestSwingCommonFeatures.<init>(TestSwingCommonFeat ures.java:28)
    at TestSwingCommonFeatures.main(TestSwingCommonFeatur es.java:41)

     import javax.swing.*;
    import java.awt.*;
    import javax.swing.border.*;
     
    public class TestSwingCommonFeatures extends JFrame
    {
    	private JPanel p1, p2;
    	private JButton jbtLeft, jbtCenter, jbtRight;
    	private JLabel jlblRed, jlblOrange;
     
    	public TestSwingCommonFeatures()
    	{
    		p1 = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
    		p1.add(jbtLeft = new JButton("Left"));
    		p1.add(jbtCenter = new JButton("Center"));
    		p1.add(jbtRight = new JButton("Right"));
    		p1.setBorder(new TitledBorder("Three Buttons"));
    		jbtLeft.setBackground(Color.WHITE);
    		jbtCenter.setForeground(Color.GREEN);
    		jbtRight.setToolTipText("This is the right button");
     
    		setLayout(new GridLayout(2,1,5,5));
    		p2.add(jlblRed = new JLabel("Red"));
    		p2.add(jlblOrange = new JLabel("Orange"));
    		p2.setBorder(new TitledBorder("Two Labels"));
    		Font largeFont = new Font("TimesRoman", Font.BOLD, 20);
    		LineBorder lineBorder = new LineBorder(Color.BLACK, 2);
     
    		setLayout(new GridLayout(2,1,5,5));
    		add(p1);
    		add(p2);		
    	}
     
    	public static void main(String[] args)
    	{
          TestSwingCommonFeatures frame = new TestSwingCommonFeatures();
          frame.setTitle("TestSwingCommonFeatures");
          frame.pack();
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
       }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,040
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: GUI won't run properly once panel is added

    Exception in thread "main" java.lang.NullPointerException
    at TestSwingCommonFeatures.<init>(TestSwingCommonFeat ures.java:28)
    There is a variable with a null value when the statement on line 28 is executed. Find the variable with the null value and then backtrack in the code to see why that variable does not have a valid value.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: GUI won't run properly once panel is added

    So one of my JPanels, JButtons or JLabels isn't correct?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    21,040
    Thanks
    52
    Thanked 2,280 Times in 2,251 Posts

    Default Re: GUI won't run properly once panel is added

    There is a variable on line 28 with a null value. Look at line 28. What variable is used on that line?
    Does that variable have a valid value? Where is that variable assigned a value?
    If you don't understand my answer, don't ignore it, ask a question.

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

    mwardjava92 (February 23rd, 2013)

  6. #5
    Member
    Join Date
    Oct 2011
    Posts
    40
    My Mood
    Sneaky
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Default Re: GUI won't run properly once panel is added

    I've finally figured it out. What a silly mistake to make! Thanks man.

Similar Threads

  1. GUI program won't take 'char' input code, can't seem to find the correct one...
    By Eclecstatic in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 1st, 2012, 07:12 AM
  2. Program won't run help!
    By iridebmxnj in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 26th, 2012, 09:00 PM
  3. [SOLVED] Beginner, stuck on implementing while loop, compiles fine but still won't run
    By GregC in forum What's Wrong With My Code?
    Replies: 5
    Last Post: March 1st, 2012, 05:36 PM
  4. Code compiles fine but doesn't run Properly
    By nadirkhan in forum What's Wrong With My Code?
    Replies: 1
    Last Post: November 9th, 2011, 11:46 AM
  5. HELP! My applet won't run on a web browser....
    By coolidge in forum Java Applets
    Replies: 4
    Last Post: October 21st, 2011, 08:52 AM