title: Change font size with ActionListener
body: <p>I need to create a Listener that changes the font size when I click on a <code>JMenuItem</code>. However, I have different labels for different parts, so when I change the font size, it throws an error at me when the labels for the other parts of the program are called but not active. Basically I can't change all the fonts at once. </p>
  
  <p>I've tried using if-statements but for some reason only the first one seems to work! The others give me a <code>nullPointerException</code>.</p>
  
  <p>Here is my listener code thus far.. </p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.Color;
  import java.awt.Font;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.event.KeyEvent;
  import java.awt.event.MouseEvent;
  import java.awt.event.MouseListener;
  import java.text.DecimalFormat;
  
  import javax.swing.ImageIcon;
  import javax.swing.JButton;
  import javax.swing.JComboBox;
  import javax.swing.JFrame;
  import javax.swing.JLabel;
  import javax.swing.JMenu;
  import javax.swing.JMenuBar;
  import javax.swing.JMenuItem;
  import javax.swing.JOptionPane;
  import javax.swing.JPanel;
  import javax.swing.JTextField;
  import javax.swing.UIManager;
  
  
  
  
  
  
  
  
  
  
  public class financeFormula extends JFrame {
  
  
  
       private JMenu fileMenu;
         private JMenu textMenu;
         private JMenuItem exitItem;
         private JMenuItem fontSize;
         private JMenuItem help;
         private JPanel presentValuePanel;  
         private JPanel financeFinderPanel;// A panel container
         private JLabel principalMessage;  
         private JLabel yearlyRateMessage;
         private JLabel termYearMessage;
         private JPanel simpleInterestPanel;
         private JPanel doublingTimePanel;
         private JPanel compoundInterestPanel;
         private JLabel NONEMessage;
         private JLabel imageLabel;
         private JLabel label;// A message to display
         private JMenuBar menuBar;
         private JTextField principalText; // To hold user input
         private JButton calcButton;       // Performs calculation
  
         private final int WINDOW_WIDTH = 600;  // Window width
         private final int WINDOW_HEIGHT = 600; // Window height
         private JTextField yearlyRateText;
         private JTextField termYearText;
         DecimalFormat dc =new DecimalFormat("####0.00"); //formater 
         private JComboBox financeBox; 
         double principal = 400.0;
         double yearlyRate = .04;
         double termYears = 4.0;
  
  
         private String[] financeFormulas = { "NONE", "Present value", "Simple interest",
               "Doubling time", "Compound interest", "Decaf"};
  
  
         //financeFormulaClass financeFormula = new financeFormulaClass(principal, yearlyRate, termYears);
  
         /**
          *  Constructor
          */
  
  
  
         public financeFormula()
         {
            // Call the JFrame constructor.
            super("Finance Class");
  
            // Set the size of the window.
            setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
  
            // Specify what happens when the close
            // button is clicked.
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
            // Build the panel and add it to the frame.
            financeFinderPanel();
            setLayout(new BorderLayout());
  
            //buildPanel();
  
            menuBar = new JMenuBar();
            //buildFileMenu();
            buildTextMenu();
            //menuBar.add(fileMenu);
            menuBar.add(textMenu);
            setJMenuBar(menuBar);
  
            // Add the panel to the frame's content pane.
            add(financeFinderPanel);
            //add(panel);
  
            // Display the window.
            setVisible(true);
         }
  
         private void financeFinderPanel()
         {
            // Create a panel to hold the combo box.
  
  
            financeFinderPanel = new JPanel();
  
            //create label
            label = new JLabel("Pick your formula");
            //ImageIcon funnyImage = new ImageIcon("funny.gif");
            //imageLabel = new JLabel();
            //imageLabel.setLocation(50, 55);
  
            label.setForeground(Color.BLUE);
            // Create the combo box
            financeBox = new JComboBox(financeFormulas);
            //imageLabel.setIcon(funnyImage);
            // Register an action listener.
            financeBox.addActionListener(new financeBoxListener());
            exitItem = new JMenuItem("Exit");   
            exitItem.setMnemonic(KeyEvent.VK_X);
  
            // Add the combo box to the panel.
            financeFinderPanel.add(financeBox);
            financeFinderPanel.add(label);
           // financeFinderPanel.add(imageLabel);
  
  
         }
  
  
         private void buildMenuBar()
         {
            // Create the menu bar.
            menuBar = new JMenuBar();
  
            // Create the file and text menus.
            //buildFileMenu();
            buildTextMenu();
  
            // Add the file and text menus to the menu bar.
            //menuBar.add(fileMenu);
            menuBar.add(textMenu);
  
            // Set the window's menu bar.
            setJMenuBar(menuBar);
         }
  
  
  
         public void buildTextMenu(){
  
             fontSize = new JMenuItem("Large font");
             fontSize.setMnemonic(KeyEvent.VK_2);
             fontSize.addActionListener(new FontListener());
             textMenu = new JMenu("Text");
             textMenu.add(fontSize);
  
  
  
         }
  
         private void presentValuePanel()
         {
            // Create the label, text field, and button components.
            principalMessage = new JLabel("principal");
            principalText = new JTextField(10);
            yearlyRateMessage = new JLabel("Yearly Rate");
            yearlyRateText = new JTextField(10);
            termYearMessage = new JLabel("Term Year");
            termYearText = new JTextField(10);
            calcButton = new JButton("Calc Present Value");
            //fontButton = new JButton("Enlarge Font");
  
            // Add an action listener to the button.
            //calcButton.addActionListener(new CalcButtonListener());
  
            //calcButton.addActionListener(new financeFormListener());
            //fontButton.addActionListener(new FontListener());
            // Create a panel to hold the components.
            presentValuePanel = new JPanel();
  
            // Add the label, text field, and button to the panel.
            presentValuePanel.add(principalMessage);
            presentValuePanel.add(principalText);
            presentValuePanel.add(yearlyRateMessage);
            presentValuePanel.add(yearlyRateText);
            presentValuePanel.add(termYearMessage);
            presentValuePanel.add(termYearText);
            presentValuePanel.add(calcButton);
            //presentValuePanel.add(fontButton);
         }
  
            private void simpleInterestPanel(){
  
                    principalMessage = new JLabel("principal");
                    principalText = new JTextField(10);
                    yearlyRateMessage = new JLabel("Yearly Rate");
                    yearlyRateText = new JTextField(10);
                    termYearMessage = new JLabel("Term Year");
                    termYearText = new JTextField(10);
                    calcButton = new JButton("Calc Simple Interest");
  
                    simpleInterestPanel = new JPanel();
                 //   calcButton.addActionListener(new financeFormListener());
  
                    simpleInterestPanel.add(principalMessage);
                    simpleInterestPanel.add(principalText);
                    simpleInterestPanel.add(yearlyRateMessage);
                    simpleInterestPanel.add(yearlyRateText);
                    simpleInterestPanel.add(termYearMessage);
                    simpleInterestPanel.add(termYearText);
                    simpleInterestPanel.add(calcButton);
  
  
  
             }
  
  
  
  
         private class financeBoxListener implements ActionListener
         {
            public void actionPerformed(ActionEvent e)
            {
  
                String selection = (String) financeBox.getSelectedItem();
                if(selection.equals("Present value")){
                    //financeFinderPanel.removeAll();
                    presentValuePanel();
                    add(presentValuePanel, BorderLayout.NORTH);
                    financeFinderPanel.removeAll();
                    pack();
  
                }
  
  
  
               else if(selection.equals("Simple interest")){
  
                   simpleInterestPanel();
                   add(simpleInterestPanel, BorderLayout.NORTH);
                   financeFinderPanel.removeAll();
                   pack();                      
                }
  
            }
         }
  
  
                private class FontListener implements ActionListener{
  
                      @Override
                      public void actionPerformed(ActionEvent e) {
                          // TODO Auto-generated method stub
  
  
                          String actionCommand = e.getActionCommand();
  
                          if(presentValuePanel.isEnabled() &amp;&amp; actionCommand.equals("Large font")){
                              principalMessage.setFont(new Font("sans-serif", Font.PLAIN, 22));
                          }
                          else if(simpleInterestPanel.isEnabled() &amp;&amp; actionCommand.equals("Large font")){
                              principalMessage.setFont(new Font("sans-serif", Font.PLAIN, 22));
                          }
  
  
                      }
                   }
  
  
         static void main(String[] args) {
          // TODO Auto-generated method stub
  
  
          new financeFormula();
  
      }
  
  }
  </code></pre>
  
  <p>NOTE: For some reason the Button enlarge font is still showing..That is not what I am asking for I am asking when I change different formulas and click the JMenuItem Large font it only works for present Value and not simple Interest..</p>