Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:16:54+00:00 2026-06-08T07:16:54+00:00

I am trying to make a simple tax calculator program in Java, and cant

  • 0

I am trying to make a simple tax calculator program in Java, and cant seem to make the calculation work right. In the code I ended up having to turn the JTextField input into a double variable and then a string. For some reason, this does not work and brings up a bunch of errors. I know there must be some sort of simpler way to write this, so any ideas would be appreciated.

import javax.swing.*;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.lang.*;


public class TaxCalculator extends JFrame {

    String twelve;
    JTextField input;
    JLabel ans;

    public TaxCalculator() {
        JFrame frame = new JFrame("Tax Calculator");
        JTextField input = new JTextField(10);
        JLabel ans = new JLabel("");
        JButton twelve = new JButton("12%");

        frame.setVisible(true);
        frame.setLayout(new FlowLayout());
        frame.setSize(250, 100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.add(new JLabel("Price of Item:"));
        frame.add(input);
        frame.add(ans);
        frame.add(twelve);

        twelve.addActionListener(new HandlerClass());

    }

    public static void main(String[] args) {
        TaxCalculator calc = new TaxCalculator();
    }

    public class HandlerClass implements ActionListener {
        public void actionPerformed(ActionEvent ae) {
            double fnum = Double.parseDouble(input.getText());

            if (ae.getSource() == twelve) {
                fnum = (fnum / 0.12) + fnum;
                ans.setText(Double.toString(fnum));
            }

        }

    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:16:55+00:00Added an answer on June 8, 2026 at 7:16 am

    The problem is you have declared JTextField input; as a class variable but also created a local variable of the same name and added it to your JFrame. That is why you’re encountering a NullPointerException when you perform double fnum = Double.parseDouble(input.getText());

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to make a simple blackjack program. Sadly, I'm having problems right off
I am trying to make simple java code that will check if a table
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple program to catalogue books. Something like this, for example:
Trying to make a simple toggle menu, and I can't seem to hide/show the
I am trying to make a simple program to convert time given in seconds
I am trying to make a simple drawing program in python using pygame. I
I'm trying to make a simple program to change IP parameters in C#. I
I'm trying to make a simple paint app. I'm having trouble with the array
Trying to make a simple jquery drop down here, here's my code so far:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.