0

There are no errors when I compile the program, but when I run it and use the "Enter" or "Clear" buttons, they do not work.

 ----jGRASP exec: java IncomeTax
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at IncomeTax.enterJButtonActionPerformed(IncomeTax.java:182)
    at IncomeTax$1.actionPerformed(IncomeTax.java:125)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
    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:6525)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6290)
    at java.awt.Container.processEvent(Container.java:2234)
    at java.awt.Component.dispatchEventImpl(Component.java:4881)
    at java.awt.Container.dispatchEventImpl(Container.java:2292)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
    at java.awt.Container.dispatchEventImpl(Container.java:2278)
    at java.awt.Window.dispatchEventImpl(Window.java:2739)
    at java.awt.Component.dispatchEvent(Component.java:4703)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:746)
    at java.awt.EventQueue.access$400(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:697)
    at java.awt.EventQueue$3.run(EventQueue.java:691)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
    at java.awt.EventQueue$4.run(EventQueue.java:719)
    at java.awt.EventQueue$4.run(EventQueue.java:717)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:716)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)






import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.*; // import formatting


public class IncomeTax extends JFrame
{
   // declarations
   Color black = new Color(0, 0, 0);
   Color white = new Color(255, 255, 255);

   DecimalFormat currency; // declare currency

   JLabel grossIncomeJLabel;
   JTextField grossIncomeJTextField;

   JLabel taxJLabel;
   JTextField taxJTextField;

   JLabel netIncomeJLabel;
   JTextField netIncomeJTextField;

   JButton enterJButton;
   JButton clearJButton;
   JButton closeJButton;

   double grossIncome;
   double tax;
   double netIncome;
   double taxRate;


      public IncomeTax()
      {
            createUserInterface();
      }

      public void createUserInterface()
      {
      Container contentPane = getContentPane();
      contentPane.setBackground(white);
      contentPane.setLayout(null);

      //initialize components
      JLabel grossIncomeJLabel;
      grossIncomeJLabel = new JLabel();
      grossIncomeJLabel.setBounds(50, 20, 150, 20);
      grossIncomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
      grossIncomeJLabel.setText("Enter Gross Income:");
      grossIncomeJLabel.setForeground(black);
      grossIncomeJLabel.setHorizontalAlignment(JLabel.LEFT);
      contentPane.add(grossIncomeJLabel);

      JTextField grossIncomeJTextField;
      grossIncomeJTextField = new JTextField();
      grossIncomeJTextField.setBounds(200, 20, 100, 20);
      grossIncomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
      grossIncomeJTextField.setHorizontalAlignment(JTextField.CENTER);
      grossIncomeJTextField.setForeground(black);
      grossIncomeJTextField.setBackground(white);
      grossIncomeJTextField.setEditable(true);
      contentPane.add(grossIncomeJTextField);

      JLabel taxJLabel;
      taxJLabel = new JLabel();
      taxJLabel.setBounds(50, 110, 150, 20);
      taxJLabel.setFont(new Font("Default", Font.PLAIN, 12));
      taxJLabel.setText("Income Tax:");
      taxJLabel.setForeground(black);
      taxJLabel.setHorizontalAlignment(JLabel.LEFT);
      contentPane.add(taxJLabel);

      JTextField taxJTextField;
      taxJTextField = new JTextField();
      taxJTextField.setBounds(200, 110, 100, 20);
      taxJTextField.setFont(new Font("Default", Font.PLAIN, 12));
      taxJTextField.setHorizontalAlignment(JTextField.CENTER);
      taxJTextField.setForeground(black);
      taxJTextField.setBackground(white);
      taxJTextField.setEditable(false);
      contentPane.add(taxJTextField);

      JLabel netIncomeJLabel;
      netIncomeJLabel = new JLabel();
      netIncomeJLabel.setBounds(50, 140, 150, 20);
      netIncomeJLabel.setFont(new Font("Default", Font.PLAIN, 12));
      netIncomeJLabel.setText("Net Income:");
      netIncomeJLabel.setForeground(black);
      netIncomeJLabel.setHorizontalAlignment(JLabel.LEFT);
      contentPane.add(netIncomeJLabel);

      JTextField netIncomeJTextField;
      netIncomeJTextField = new JTextField();
      netIncomeJTextField.setBounds(200, 140, 100, 20);
      netIncomeJTextField.setFont(new Font("Default", Font.PLAIN, 12));
      netIncomeJTextField.setHorizontalAlignment(JTextField.CENTER);
      netIncomeJTextField.setForeground(black);
      netIncomeJTextField.setBackground(white);
      netIncomeJTextField.setEditable(false);
      contentPane.add(netIncomeJTextField);


      // control
      enterJButton = new JButton();
      enterJButton.setBounds(25, 300, 100, 20);
      enterJButton.setFont(new Font("Default", Font.PLAIN, 12));
      enterJButton.setText("Enter");
      enterJButton.setForeground(black);
      enterJButton.setBackground(white);
      contentPane.add(enterJButton);
      enterJButton.addActionListener(

         new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
               enterJButtonActionPerformed(event);
            }
         }
      );

      clearJButton = new JButton();
      clearJButton.setBounds(150, 300, 100, 20);
      clearJButton.setFont(new Font("Default", Font.PLAIN, 12));
      clearJButton.setText("Clear");
      clearJButton.setForeground(black);
      clearJButton.setBackground(white);
      contentPane.add(clearJButton);
      clearJButton.addActionListener(

         new ActionListener()
         {
            public void actionPerformed(ActionEvent event)
            {
               clearJButtonActionPerformed(event);
            }
         }
      );

      closeJButton = new JButton();
      closeJButton.setBounds(275, 300, 100, 20);
      closeJButton.setFont(new Font("Default", Font.PLAIN, 12));
      closeJButton.setText("Close");
      closeJButton.setForeground(black);
      closeJButton.setBackground(white);
      contentPane.add(closeJButton);
      closeJButton.addActionListener(

         new ActionListener()
        {
            public void actionPerformed(ActionEvent event)
            {
               closeJButtonActionPerformed(event);
            }
         }
      );

      // set properties of application’s window
      setTitle("Income Tax"); // set title bar text
      setSize(400, 400);    // set window size
      setVisible(true);     // display window
      }

      // main method
      public static void main(String[] args)
      {
            IncomeTax application = new IncomeTax();
            application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      }

      public void enterJButtonActionPerformed(ActionEvent event)
   {
      // get input values
        grossIncome = Double.parseDouble(grossIncomeJTextField.getText());

        // calculate values
      taxRate = .20;
      tax = grossIncome * taxRate;
        netIncome = grossIncome - tax;

        // display results
      currency = new DecimalFormat("$0.00");
      taxJTextField.setText("" + currency.format(tax));
      netIncomeJTextField.setText("" + currency.format(netIncome));

    }

   public void clearJButtonActionPerformed(ActionEvent event)
   {
      grossIncomeJTextField.setText("");
      grossIncomeJTextField.requestFocusInWindow();
      taxJTextField.setText("");
      netIncomeJTextField.setText("");

   }
    public void closeJButtonActionPerformed(ActionEvent event)
   {
      System.exit(0);
   }    



}
2
Contributors
1
Reply
15
Views
2 Years
Discussion Span
Last Post by stultuske
1

look at line 182 in your code (it's pretty hard to see above which line it is).
it seems like you are calling a method on an instance variable you haven't instantiated yet.

and that field would be "grossIncomeJTextField".

the reason for this is: in your createUserInterface method, you are hiding the actual field behind a local field, which can 't be used in your other methods.

go to your createUserInterface method, and look for the following

JTextField grossIncomeJTextField;
grossIncomeJTextField = new JTextField();

delete that first line, and it will use the instance variable you need.

do this for the other fields as well, and your code 'll run just the way it should.

Votes + Comments
Thank you so so much it worked! As a beginner I was getting so frustrated with this assignment. Thanks for taking the time to respond!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.