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

Thread: add a 3rd tab to my JPane and now i get an exception error when i run my code??

  1. #1
    Junior Member
    Join Date
    May 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default add a 3rd tab to my JPane and now i get an exception error when i run my code??

    I've worked really hard on this and now i'm totally stressing because i'm sure its just something simple with my layout. The code itself is definately working because i can run the program with one tab just fine. I only have a problem when i add in the next tab.

    This is the error i receive at run time
    Exception in thread "main" java.lang.NullPointerException
    at java.awt.Container.addImpl(Container.java:1045)
    at java.awt.Container.add(Container.java:365)
    at PersonalPanel.<init>(PersonalPanel.java:126)
    at GameBoxLayout.main(GameBoxLa

    I had two tabs, Aboutpanel.java that is all text and a second tab MadlibPanel.javathat uses the East,West,North South layout. Program compiled fine and ran well. I added on a 3rd tab (Personalpanel.java) that also uses teh East,West,North South layout and now i'm getting the above exception error when i try to run it. I would imagine that it somehow does not like the fact that i am adding a nother tab with the east/west/north layout??
    Here's my code - This is a final project and this is the only thing i have left to do so i'm totally stressing out. (The program works fine outside of this, as panel 2 and 3 use the same exact code, just different variables.)

    import java.awt.*;
    import javax.swing.*;
     
    public class GameBoxLayout
    {
     
    //create a frame that contains a 3 tabbed pane.  
     
    	public static void main (String[] args)
     
    	{
    	//Use the Java look and feel instead of the windwos look and feel on JFrame.
    		JFrame.setDefaultLookAndFeelDecorated(true);
     
    	//Create JFrame
    		JFrame frame = new JFrame ("Mad Libs Game Box");
    		frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
     
    	//Create tabbed panes for JFrame	
    		JTabbedPane tp = new JTabbedPane();
    		tp.addTab ("Hamlet", new MadlibPanel());
    		tp.addTab ("About MadLibs", new AboutPanel());
    		tp.addTab ("Personal Ad", new PersonalPanel());
    //		tp.addTab ("Grammar Lesson", new GrammarPanel());
     
    		frame.getContentPane().add(tp);
    		frame.setSize(900,600);
     
     
    		frame.setVisible(true);
    	}
    }

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.BorderFactory; 
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.EtchedBorder;
     
     
    public class MadlibPanel extends JPanel
    {
     
    // Set global class variables
     
     
    	private JLabel resultLabel, verb1Input, adj1Input, verb2Input,nounpInput;
    	private JLabel weaponpInput, adj2Input, noun1Input, foodpInput, verb3Input, adj3Input, noun2Input;
    	private JLabel verb4Input, verbedInput, noun4Input, adj4Input, noun3Input;
       private JTextField fahrenheit, verb1, adj1, verb2, nounp, weaponp, adj2, noun1, foodp, verb3, adj3, noun2;
       private JTextField verb4, verbed, noun4, adj4, noun3;
    	JButton Display;		
    	JTextArea ta;
    	JScrollPane sp;
    	JPanel eastPanel, centerPanel, westPanel, northPanel, panel;
    	String line, line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line12, line13, line14;
     
     
     
     //  Constructor: Sets up the main GUI components.
     
       public MadlibPanel()
       {
     
     
    //apply decorative border to the frame
     
          Border paneEdge = BorderFactory.createEmptyBorder(0,10,10,10);
    	   Border etched;
     
      	   etched = BorderFactory.createEtchedBorder();
     
     
    //creates JPanels  
           panel = new JPanel();
           panel.setLayout(new BorderLayout());
           panel.setBackground (Color.cyan);
           panel.setPreferredSize(new Dimension(900,500));  
    	   eastPanel = new JPanel();
    	   eastPanel.setLayout(new GridLayout(16,2));
    	   eastPanel.setBackground(Color.pink);
    	   centerPanel = new JPanel();
    	   centerPanel.setBackground(Color.pink);
    	   westPanel = new JPanel();
    	   westPanel.setBackground(Color.pink);
    	   northPanel = new JPanel();
    	   northPanel.setBackground(Color.pink);
     
     
     
    //Creaet a titled border
    TitledBorder title1;
    title1 = BorderFactory.createTitledBorder(etched, "MADLIBS as written by Shakespeare");
    setBorder(title1);
     
     
    //Define text fields and Jlabels to request user input.
    //Define each field to a lenght of 15 for consistancy.
     
    		verb1Input = new JLabel ("Enter a verb");
    		verb1 = new JTextField (15);  
    		adj1Input = new JLabel ("Enter an adjective");
    		adj1 = new JTextField (15);  
    		verb2Input = new JLabel ("Enter a verb");
    		verb2 = new JTextField (15);  
    		nounpInput = new JLabel ("Enter a noun (plural)");
    		nounp = new JTextField (15);  
    		weaponpInput = new JLabel ("Enter a weapon (plural)");
    		weaponp = new JTextField (15);  
    		adj2Input = new JLabel ("Enter an adjective");
    		adj2 = new JTextField (15);  
    		noun1Input = new JLabel ("Enter a noun");
    		noun1 = new JTextField (15);  
    		foodpInput = new JLabel ("Enter a food (plural)");
    		foodp = new JTextField (15);  
    		verb3Input = new JLabel ("Enter a verb");
    		verb3 = new JTextField (15);  
    		adj3Input = new JLabel ("Enter an adjective");
    		adj3 = new JTextField (15);  
    		noun2Input = new JLabel ("Enter a noun");
    		noun2 = new JTextField (15);  
    		verb4Input = new JLabel ("Enter a verb");
    		verb4 = new JTextField (15);  
    		noun3Input = new JLabel ("Enter a noun");
    		noun3 = new JTextField (15);  
    		verbedInput = new JLabel ("Enter a verb (ending in ed)");
    		verbed = new JTextField (15);  
    		noun4Input = new JLabel ("Enter a noun");
    		noun4 = new JTextField (15);  
    		adj4Input = new JLabel ("Enter an adjective");
    		adj4 = new JTextField (15);  
     
     
         	adj4.addActionListener (new TempListener()); 
    		Display = new JButton ("Display");			//initializes my new button
    		Display.addActionListener (new TempListener());	//creates a new instance 
     
     
    //add text fields and Jlabels to request user input.
    //add each field to a lenght of 15 for consistancy.
     
    		eastPanel.add (verb1Input);
    		eastPanel.add (verb1);
    		eastPanel.add (adj1Input);
    		eastPanel.add (adj1);
    		eastPanel.add (verb2Input);
    		eastPanel.add (verb2);
    		eastPanel.add (nounpInput);
    		eastPanel.add (nounp);
    		eastPanel.add (weaponpInput);
    		eastPanel.add (weaponp);
    		eastPanel.add (adj2Input);
    		eastPanel.add (adj2);
    		eastPanel.add (noun1Input);
    		eastPanel.add (noun1);
    		eastPanel.add (foodpInput);
    		eastPanel.add (foodp);
    		eastPanel.add (verb3Input);
    		eastPanel.add (verb3);
    		eastPanel.add (adj3Input);
    		eastPanel.add (adj3);
    		eastPanel.add (noun2Input);
    		eastPanel.add (noun2);
    		eastPanel.add (noun2Input);
    		eastPanel.add (noun2);
    		eastPanel.add (verb4Input);
    		eastPanel.add (verb4);
    		eastPanel.add (verbedInput);
    		eastPanel.add (verbed);
    		eastPanel.add (noun4Input);
    		eastPanel.add (noun4);
    		eastPanel.add (adj4Input);
    		eastPanel.add (adj4);
       	eastPanel.add (Display);
     
    //define text area to panel with specified size of 30,40
    //define scroll pane to the panel
    //add the center panel
            ta = new JTextArea("Your story will appear here", 30, 40);
            sp = new JScrollPane(ta);
            centerPanel.add(sp);
     
    //add individual panels to the frame.
     
            panel.add (eastPanel, BorderLayout.EAST);
            panel.add (centerPanel, BorderLayout.CENTER);
            panel.add (westPanel, BorderLayout.WEST);
            panel.add (northPanel, BorderLayout.NORTH);
     
    //add main panel to the frame        
            add (panel);
     
       }
     
     
     
    //  ActionListener defined for display button
     
       private class TempListener implements ActionListener
       {
     
     
    //  concatenates user input to the madlib story 
     
          public void actionPerformed (ActionEvent event)
          {
     
     
    		  line1 = "To " + verb1.getText() + ", or not to " + verb1.getText() + ",--that is the question:--\n";
    	     line2 = "Whether tis more " + adj1.getText() + " in the mind to " + verb2.getText() + "\n";
    		  line3 = "The " + nounp.getText() + " and " + weaponp.getText() + " of " + adj2.getText() + " fortune\n";
    		  line4 = "Or to take " + noun1.getText() + " against a sea of " + foodp.getText() + ",\n";
    		  line5 = "And by opposing end them?--To die,--to " + verb3.getText() + ",--\n";
    		  line6 = "No more; and by a " + verb3.getText() + " to say we end \n";
    		  line7 = "The heartache, and the thousand " + adj3.getText() + " shocks\n";
    		  line8 = "That flesh is heir to,--`tis a " + noun2.getText() + "\n";
    		  line9 = "Devoutly to be wish`d. To die,--to " + verb4.getText() + ";--\n";
    		  line10 = "To " + verb4.getText() + "! perchance to dream:--ay, there`s the " + noun3.getText() +"\n";
    		  line11 = "For in that " + verb4.getText() + " of death what dreams may come,\n";
    		  line12 = "When we have " + verbed.getText() + " off this mortal " + noun4.getText()+ "\n";
    		  line13 = "Must give us pause: there`s the respect\n";
    		  line14 = "That makes calamity of so " + adj4.getText() + " life...\n"; 
            line = line1+line2+line3+line4+line5+line6+line7+line8+line9+line10+line11+line12+line13+line14;
     
    //sets the value of the text area to be the madlib output
     
    	      ta.setText(line);
     
    	 }
      }
    }
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.BorderFactory; 
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.EtchedBorder;
     
     
    public class PersonalPanel extends JPanel
    {
     
    // Set global class variables
     
     
    	private JTextField liquid, famous, place, job, nationality, famousg, girlfriend, num;
    	private JTextField nounp1, nounp2, nounp3, noun1, noun2;
    	private JTextField adj1, adj2;
    	private JTextField verbed;
    	private JLabel verbedInput, adj1Input, adj2Input, nounp1Input, nounp2Input, nounp3Input, noun1Input;
    	private JLabel noun2Input, liquidInput, famousInput, placeInput, jobInput, nationalityInput;
    	private JLabel famousgInput, girlfriendInput, numInput, resultLabel;
    	JButton Display;		
    	JTextArea ta;
    	JScrollPane sp;
    	JPanel eastPanel, centerPanel, westPanel, northPanel, panel;
    	String line, line1, line2, line3, line4, line5, line6, line7, line8, line9, line10;
     
     
     
     //  Constructor: Sets up the main GUI components.
     
       public PersonalPanel()
       {
     
     
    //apply decorative border to the frame
     
          Border paneEdge = BorderFactory.createEmptyBorder(0,10,10,10);
    	   Border etched;
     
      	   etched = BorderFactory.createEtchedBorder();
     
     
    //creates JPanels  
           panel = new JPanel();
           panel.setLayout(new BorderLayout());
           panel.setBackground (Color.cyan);
           panel.setPreferredSize(new Dimension(900,500));  
    	   eastPanel = new JPanel();
    	   eastPanel.setLayout(new GridLayout(16,2));
    	   eastPanel.setBackground(Color.pink);
    	   centerPanel = new JPanel();
    	   centerPanel.setBackground(Color.pink);
    	   westPanel = new JPanel();
    	   westPanel.setBackground(Color.pink);
    	   northPanel = new JPanel();
    	   northPanel.setBackground(Color.pink);
     
     
     
    //Creaet a titled border
    TitledBorder title1;
    title1 = BorderFactory.createTitledBorder(etched, "MADLIBS as written by Shakespeare");
    setBorder(title1);
     
     
    //Define text fields and Jlabels to request user input.
    //Define each field to a lenght of 15 for consistancy.
     
    		adj1Input = new JLabel ("Enter an adjective");
    		adj1 = new JTextField (15);  
    		verbedInput = new JLabel ("Enter a verb ending in ed");
    		verbed = new JTextField (15);  
    		nounp1Input = new JLabel ("Enter a noun (plural)");
    		noun1 = new JTextField (15);  
    		liquidInput = new JLabel ("Enter a liquid");
    		liquid = new JTextField (15);  
    		nounp2Input = new JLabel ("Enter a noun (plural)");
    		nounp2 = new JTextField (15);  
    		famousInput = new JLabel ("Enter a famous person");
    		famous = new JTextField (15);  
    		placeInput = new JLabel ("Enter a place");
    		place = new JTextField (15);  
    		jobInput = new JLabel ("Enter an occupation");
    		job = new JTextField (15);  
    		noun1Input = new JLabel ("Enter a noun");
    		noun1 = new JTextField (15);  
    		nationalityInput = new JLabel ("Enter an nationality");
    		nationality = new JTextField (15);  
    		famousgInput = new JLabel ("Enter a female celebrity");
    		famousg = new JTextField (15);  
    		noun2Input = new JLabel ("Enter a noun");
    		noun2 = new JTextField (15);  
    		girlfriendInput = new JLabel ("Enter a female friend");
    		girlfriend = new JTextField (15);  
    		nounp3Input = new JLabel ("Enter a noun (plural)");
    		nounp3 = new JTextField (15);  
    		numInput = new JLabel ("Enter a number");
    		num = new JTextField (15);  
    		adj2Input = new JLabel ("Enter an adjective");
    		adj2 = new JTextField (15);  
     
     
         	adj2.addActionListener (new TempListener()); 
    		Display = new JButton ("Display");			//initializes my new button
    		Display.addActionListener (new TempListener());	//creates a new instance 
     
     
    //add text fields and Jlabels to request user input.
    //add each field to a lenght of 15 for consistancy.
     
    		eastPanel.add (adj1Input);
    		eastPanel.add (adj1);
    		eastPanel.add (verbedInput);
    		eastPanel.add (verbed);
    		eastPanel.add (nounp1Input);
    		eastPanel.add (nounp1);
    		eastPanel.add (liquidInput);
    		eastPanel.add (liquid);
    		eastPanel.add (nounp2Input);
    		eastPanel.add (nounp2);
    		eastPanel.add (famousInput);
    		eastPanel.add (famous);
    		eastPanel.add (placeInput);
    		eastPanel.add (place);
    		eastPanel.add (jobInput);
    		eastPanel.add (job);
    		eastPanel.add (noun1Input);
    		eastPanel.add (noun1);
    		eastPanel.add (nationalityInput);
    		eastPanel.add (nationality);
    		eastPanel.add (famousgInput);
    		eastPanel.add (famousg);
    		eastPanel.add (noun2Input);
    		eastPanel.add (noun2);
    		eastPanel.add (girlfriendInput);
    		eastPanel.add (girlfriend);
    		eastPanel.add (nounp3Input);
    		eastPanel.add (nounp3);
    		eastPanel.add (numInput);
    		eastPanel.add (num);
    		eastPanel.add (adj2Input);
    		eastPanel.add (adj2);
       	eastPanel.add (Display);
     
    //define text area to panel with specified size of 30,40
    //define scroll pane to the panel
    //add the center panel
            ta = new JTextArea("Your story will appear here", 30, 40);
            sp = new JScrollPane(ta);
            centerPanel.add(sp);
     
    //add individual panels to the frame.
     
            panel.add (eastPanel, BorderLayout.EAST);
            panel.add (centerPanel, BorderLayout.CENTER);
            panel.add (westPanel, BorderLayout.WEST);
            panel.add (northPanel, BorderLayout.NORTH);
     
    //add main panel to the frame        
            add (panel);
     
       }
     
     
     
    //  ActionListener defined for display button
     
       private class TempListener implements ActionListener
       {
     
     
    //  concatenates user input to the madlib story 
     
          public void actionPerformed (ActionEvent event)
          {
     
     
     
     
    		line1 = "I enjoy long, " + adj1.getText() + " walks on the beach, getting " + verbed.getText()+ "\n";
    		line2 = "in the rain and serendipitous encounters with " + nounp1.getText() + ". I really\n"; 
    		line3 = "like piña coladas mixed with liquid, and romantic, candle-lit " + nounp2.getText() + "\n";
    		line4 = ". I am well-read from Dr. Seuss to " + famous.getText() + ". I travel frequently,\n";
    		line5 = "especially to " + place.getText() + ", when I am not busy with work. (I am a " + job.getText() + ".) I am \n";
    		line6 = "looking for " + noun1.getText() + " and beauty in the form of a " + nationality.getText() + " goddess.\n";
    		line7 = "She should have the physique of " + famousg.getText() + " and the " + noun2.getText() + " of \n";
    		line8 = girlfriend.getText() + ". I would prefer if she knew how to cook, clean, and wash my" + "\n";
    		line9 = noun2.getText() + ". I know I’m not very attractive in my picture, but it was taken" + "\n";
    		line10 = num.getText() + " days ago, and I have since become more " + adj2.getText() + ".\n"; 
    		line = line1+line2+line3+line4+line5+line6+line7+line8+line9+line10;
     
    //sets the value of the text area to be the madlib output
     
    	      ta.setText(line);
     
    	 }
      }
    }
    import java.awt.*;
    import javax.swing.*;
    import javax.swing.border.*;
    import javax.swing.BorderFactory; 
    import javax.swing.border.Border;
    import javax.swing.border.TitledBorder;
    import javax.swing.border.EtchedBorder;
    import javax.swing.ImageIcon;
    import javax.swing.JTabbedPane;
    import javax.swing.JLabel;
    import javax.swing.JPanel; 
    import javax.swing.JFrame;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
     
     
     public class AboutPanel extends JPanel
     { 
     
     //define the panel and its labels
     
     	public AboutPanel()
    	{
    		     super(new GridLayout(1,0));
     
          Border paneEdge = BorderFactory.createEmptyBorder(0,10,10,10);
    		Border etched;
     
       	etched = BorderFactory.createEtchedBorder();
     
    //creates JPanel    
    		  JPanel simpleBorders = new JPanel();
            simpleBorders.setBorder(paneEdge);
            simpleBorders.setLayout(new BoxLayout(simpleBorders,
                                                  BoxLayout.Y_AXIS));
     
    //Titled borders
    TitledBorder title1, title2, title3, title4, title5;
    title1 = BorderFactory.createTitledBorder(etched, "ALL ABOUT MADLIBS");
    setBorder(title1);
     
    //Sets background Colors
    		Color color = new Color(245,222,179);
    		setBackground (color);
     
    		setLayout (new BoxLayout(this, BoxLayout.Y_AXIS));
     
    		JLabel l1 = new JLabel ("Mad Libs (from ad lib, a spontaneous improvisation) is a phrasal template word game where one player");
    		JLabel blank1 = new JLabel (" ");
    		JLabel l1a = new JLabel ("prompts another for a list of words to substitute for blanks in a story; these word substitutions have");
    		JLabel blank1a = new JLabel (" ");
    		JLabel l1b = new JLabel ("a humorous effect when the resulting story is then read aloud.");
    		JLabel blank1b = new JLabel (" ");
    		JLabel l2 = new JLabel ("  ");
    		JLabel blank2 = new JLabel (" ");
    //		JLabel l2a = new JLabel (" ");		//reserved spaces
    //		JLabel blank2a = new JLabel (" ");  //reserved spaces
     
    		JLabel l3 = new JLabel ("Mad Libs was invented in 1953 by Leonard Stern and Roger Price, who published the first Mad Libs book");
    		JLabel blank3 = new JLabel (" ");
    		JLabel l3a = new JLabel ("themselves in 1958.");
    		JLabel blank3a = new JLabel (" ");
     
     
    		JLabel l4 = new JLabel ("Mad Libs consist of a book that has a short story on each page, but with many of the key words");
    		JLabel blank4 = new JLabel (" ");
    		JLabel l4a = new JLabel ("replaced with blanks. Beneath each blank is specified a lexical or other category, such as noun,");
    		JLabel blank4a = new JLabel (" ");
    		JLabel l4b = new JLabel ("verb, place, or a part of the body.  One player asks the other players, in turn, to contribute");
    		JLabel blank4b = new JLabel (" ");
    		JLabel l4c = new JLabel ("some word for the specified type for each blank, but without revealing the context for that word.");
    		JLabel blank4c = new JLabel (" ");
    		JLabel l4d = new JLabel ("Finally, the completed story is read aloud. The result is usually comic, surreal and somewhat");
    		JLabel blank4d = new JLabel (" ");
    		JLabel l4e = new JLabel ("nonsensical.");
    		JLabel blank4e = new JLabel (" ");
    		JLabel l4f = new JLabel ("");
    		JLabel blank4f = new JLabel (" ");
     
     
    		ImageIcon imageIcon = new ImageIcon("madlibLogo4.gif");
     	   JLabel pic1 = new JLabel(imageIcon);
     
     
    		add (l1);
    		add (blank1);
    		add (l1a);
    		add (blank1a);
    		add (l1b);
    		add (blank1b);
     		add (l2);
    		add (blank2);
    	//	add (l2a);
    	//	add (blank2a);
    		add (l3);
    		add (blank3);
    		add (l3a);
    		add (blank3a);
    		add (l4);
    		add (blank4);
    		add (l4a);
    		add (blank4a);
    		add (l4b);
    		add (blank4b);
    		add (l4c);
    		add (blank4c);
    		add (l4d);
    		add (blank4d);
    		add (l4e);
    		add (blank4e);
    		add (l4f);
    		add (blank4f);
    		add (pic1);
     
     
    	}
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,358
    Thanks
    182
    Thanked 834 Times in 777 Posts
    Blog Entries
    5

    Default Re: add a 3rd tab to my JPane and now i get an exception error when i run my code??

    Check the line in your PersonalPanel that the stack trace give you...it points to one of your components you are trying to add as being null (eg....noun1p).

Similar Threads

  1. Replies: 1
    Last Post: March 31st, 2010, 10:42 PM
  2. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  3. help me out with the following code to solve the error
    By romilc in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 11th, 2009, 04:45 AM
  4. Error of "ClassNotFound Exception"
    By multicoder in forum Exceptions
    Replies: 3
    Last Post: July 1st, 2009, 03:58 PM
  5. Getting Null Pointer Exception in runtime
    By RoadRunner in forum Exceptions
    Replies: 1
    Last Post: April 26th, 2009, 02:21 PM