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: Null Pointer Exception Help!!

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Null Pointer Exception Help!!

    Hi, I'm trying to build a two classes:
    1) One to add the panels
    2) The panel class

    The code compiles fine, but when i run it, i get a null pointer exception on line 52 of TestFrame (the class)

     import java.awt.BorderLayout;
     import java.awt.Font;
     import java.awt.GridLayout;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.ButtonGroup;
     import javax.swing.JButton;
     import javax.swing.JCheckBox;
     import javax.swing.JComboBox;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JPanel;
     import javax.swing.JRadioButton;
     import javax.swing.border.EtchedBorder;
     import javax.swing.border.TitledBorder;
     
     /**
        This frame contains a text field and a control panel
        to change the font of the text.
     */
     public class TestFrame extends JFrame
     {
        /**
           Constructs the frame.
        */
        public TestFrame()
        {  
           // Construct text sample
           sampleField = new JLabel("Big Java");
           add(sampleField, BorderLayout.CENTER);
     
           // This listener is shared among all components
           class ChoiceListener implements ActionListener
           {  
              public void actionPerformed(ActionEvent event)
              {  
                 setSampleFont();
              }
           }
           listener = new ChoiceListener();
     
           createControlPanel();
           setSampleFont();
           setSize(FRAME_WIDTH, FRAME_HEIGHT);
        }
     
        /**
           Creates the control panel to change the font.
        */
        public void createControlPanel()
        {
           JPanel facenamePanel = comboBox.createComboBox();
     
           // Line up component panels
     
           JPanel controlPanel = new JPanel();
           controlPanel.setLayout(new GridLayout(3, 1));
           controlPanel.add(facenamePanel);
     
           // Add panels to content pane
     
           add(controlPanel, BorderLayout.SOUTH);
        }
     
         /**
           Gets user choice for font name, style, and size
           and sets the font of the text sample.
        */
        public void setSampleFont()
        {  
           // Get font name   
           String facename 
                 = (String) facenameCombo.getSelectedItem();
     
           // Set font of text field
     
           sampleField.setFont(new Font(facename,Font.BOLD,32));      
           sampleField.repaint();
        }
     
        private JLabel sampleField;
        private JComboBox facenameCombo;
        private ActionListener listener;
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 400;
     
    	 private ComboBox comboBox;
     }


     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.JComboBox;
     import javax.swing.JPanel;
     
     /**
        This frame contains a text field and a control panel
        to change the font of the text.
     */
     public class ComboBox extends JPanel
     {
     
        /**
           Creates the combo box with the font style choices.
           @return the panel containing the combo box
        */
        public JPanel createComboBox()
        {
           facenameCombo = new JComboBox();
           facenameCombo.addItem("Serif");
           facenameCombo.addItem("SansSerif");
           facenameCombo.addItem("Monospaced");
           facenameCombo.setEditable(true);
           facenameCombo.addActionListener(listener);
     
           JPanel panel = new JPanel();
           panel.add(facenameCombo);
           return panel;
        }
     
        private JComboBox facenameCombo;
        private ActionListener listener;
     }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception Help!!

    For future reference, since there is no line numbering available, it helps to somehow flag or write out the line where the exception occurs.
    In your case, it happens here:
    JPanel facenamePanel = comboBox.createComboBox();

    I do not see where the comboBox was instantiated.

  3. The Following User Says Thank You to copeg For This Useful Post:

    puzzledstudent (November 11th, 2010)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception Help!!

    Sorry about the line numbers...

    I'm sort of new to Java Programming, but what do you mean by instantiating comboBox?

    Do I add this?

    ComboBox comboBox = new ComboBox();


    (again..sorry if i'm asking a question that seems obvious..)
    but then i get a Nullpointer exception for line 74...

    so that my code is now:
     1  import java.awt.BorderLayout;
     2  import java.awt.Font;
     3  import java.awt.GridLayout;
     4  import java.awt.event.ActionEvent;
     5  import java.awt.event.ActionListener;
     6  import javax.swing.ButtonGroup;
     7  import javax.swing.JButton;
     8  import javax.swing.JCheckBox;
     9  import javax.swing.JComboBox;
    10  import javax.swing.JFrame;
    11  import javax.swing.JLabel;
    12  import javax.swing.JPanel;
    13  import javax.swing.JRadioButton;
    14  import javax.swing.border.EtchedBorder;
    15  import javax.swing.border.TitledBorder;
    16  
    17  /**
    18     This frame contains a text field and a control panel
    19     to change the font of the text.
    20  */
    21  public class TestFrame extends JFrame
    22  {
    23     /**
    24        Constructs the frame.
    25     */
    26     public TestFrame()
    27     {  
    28        // Construct text sample
    29        sampleField = new JLabel("Big Java");
    30        add(sampleField, BorderLayout.CENTER);
    31  
    32        // This listener is shared among all components
    33        class ChoiceListener implements ActionListener
    34        {  
    35           public void actionPerformed(ActionEvent event)
    36           {  
    37              setSampleFont();
    38           }
    39        }
    40        listener = new ChoiceListener();
    41  
    42        createControlPanel();
    43        setSampleFont();
    44        setSize(FRAME_WIDTH, FRAME_HEIGHT);
    45     }
    46  
    47     /**
    48        Creates the control panel to change the font.
    49     */
    50     public void createControlPanel()
    51     {
    [COLOR="Blue"]52        ComboBox comboBox = new ComboBox();[/COLOR]
    53 
    54        JPanel facenamePanel = comboBox.createComboBox();
    55  
    56        // Line up component panels
    57  
    58        JPanel controlPanel = new JPanel();
    59        controlPanel.setLayout(new GridLayout(3, 1));
    60        controlPanel.add(facenamePanel);
    61  
    62        // Add panels to content pane
    63  
    64        add(controlPanel, BorderLayout.SOUTH);
    65     }
    66   
    67      /**
    68        Gets user choice for font name, style, and size
    69        and sets the font of the text sample.
    70     */
    71     public void setSampleFont()
    72     {  
    73        // Get font name   
    [COLOR="Blue"]74        String facename 
    75              = (String) facenameCombo.getSelectedItem();[/COLOR]
    76        
    77        // Set font of text field
    78        
    79        sampleField.setFont(new Font(facename,Font.BOLD,32));      
    80        sampleField.repaint();
    81     }
    82     
    83     private JLabel sampleField;
    84     private JComboBox facenameCombo;
    85     private ActionListener listener;
    86     private static final int FRAME_WIDTH = 300;
    87     private static final int FRAME_HEIGHT = 400;
    88     
    89     private ComboBox comboBox;
    90  }

  5. #4
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Null Pointer Exception Help!!

    You don't necessarily have to add line numbers, just highlight the line the exception is being thrown on. It helps tremendously in locating the error.
    Do I add this?

    ComboBox comboBox = new ComboBox();
    Kind of...you want to instantiate the instance variable, when you do the above you create a local variable that looses scope as soon as the createControlPanel() exits. Same goes for the other facenameCombo variable. You want to instantiate the instance variable:
    comboBox = new ComboBox();

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

    puzzledstudent (November 11th, 2010)

  7. #5
    Junior Member
    Join Date
    Nov 2010
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Null Pointer Exception Help!!

    Thanks for the info!

    My program runs fine after I edit the code, but now my font does not change when i try to select a different font.

     import java.awt.BorderLayout;
     import java.awt.Font;
     import java.awt.GridLayout;
     import java.awt.event.ActionEvent;
     import java.awt.event.ActionListener;
     import javax.swing.ButtonGroup;
     import javax.swing.JButton;
     import javax.swing.JCheckBox;
     import javax.swing.JComboBox;
     import javax.swing.JFrame;
     import javax.swing.JLabel;
     import javax.swing.JPanel;
     import javax.swing.JRadioButton;
     import javax.swing.border.EtchedBorder;
     import javax.swing.border.TitledBorder;
     
     /**
        This frame contains a text field and a control panel
        to change the font of the text.
     */
     public class TestFrame extends JFrame
     {
        /**
           Constructs the frame.
        */
        public TestFrame()
        {  
           // Construct text sample
           sampleField = new JLabel("Big Java");
           add(sampleField, BorderLayout.CENTER);
     
           // This listener is shared among all components
           class ChoiceListener implements ActionListener
           {  
              public void actionPerformed(ActionEvent event)
              {  
                 setSampleFont();
              }
           }
           listener = new ChoiceListener();
     
    		 comboBox = new ComboBox();
      	 	 facenameCombo = new JComboBox();
     
           createControlPanel();
           setSampleFont();
           setSize(FRAME_WIDTH, FRAME_HEIGHT);
        }
     
        /**
           Creates the control panel to change the font.
        */
        public void createControlPanel()
        {
     
           JPanel facenamePanel = comboBox.createComboBox();
     
           // Line up component panels
     
           JPanel controlPanel = new JPanel();
           controlPanel.setLayout(new GridLayout(3, 1));
           controlPanel.add(facenamePanel);
     
           // Add panels to content pane
     
           add(controlPanel, BorderLayout.SOUTH);
        }
     
         /**
           Gets user choice for font name, style, and size
           and sets the font of the text sample.
        */
        public void setSampleFont()
        {  
           // Get font name   
           String facename 
                 = (String) facenameCombo.getSelectedItem();
     
           // Set font of text field
     
           sampleField.setFont(new Font(facename,Font.BOLD,32));      
           sampleField.repaint();
        }
     
        private JLabel sampleField;
        private JComboBox facenameCombo;
        private ActionListener listener;
        private static final int FRAME_WIDTH = 300;
        private static final int FRAME_HEIGHT = 400;	 
        private ComboBox comboBox;
     }

    this is the test code:
     import javax.swing.JFrame;
     
     /**
        This program allows the user to view font effects.
     */
     public class TestViewer
     {  
        public static void main(String[] args)
        {  
           JFrame frame = new TestFrame();
           frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
           frame.setTitle("FontViewer");
           frame.setVisible(true);      
        }
     }

  8. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Null Pointer Exception Help!!

    Did you ever try adding an actionListener to something and having the actionListener call the method that sets it to bold or whatever?

Similar Threads

  1. [SOLVED] Null Pointer Exception
    By musasabi in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 11th, 2010, 09:25 PM
  2. HttpURLConnection null pointer exception
    By chopficaro in forum Java Theory & Questions
    Replies: 0
    Last Post: May 10th, 2010, 10:19 AM
  3. Null pointer exception
    By Wrathgarr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 23rd, 2010, 12:48 AM
  4. JComboBox null pointer Exception error
    By F_Arches in forum AWT / Java Swing
    Replies: 2
    Last Post: November 29th, 2009, 02:32 PM
  5. Null Pointer Exception
    By MysticDeath in forum Exceptions
    Replies: 2
    Last Post: October 24th, 2009, 01:49 PM