user1801794
user1801794

Reputation: 25

actionperformed firing when button pressed

Okay, first time asking a question usually i can find these kinds of answers doing a quick search, this one just has me too frustrated. It's a simple applet that takes hours worked and pay rate to get gross and net pay, most of this is just my trial run (the amount being taken out for net pay etc) but before i go any further i want to get it working.

when the button is pressed nothing happens, not exactly sure why. (More recently when i was trying this-and-that i started getting an NullPointerException in IE, probably has nothing to do with my issues but just throwing that out there too.) ANY help is appreciated, ive tried a few things but im definitely rusty on java!

heres the code:

import javax.swing.*;      
import java.awt.*;     
import java.awt.event.*; 

public class ButtonGrossPay extends JApplet implements ActionListener
{

 private JTextField txtHourWork;
 private JTextField txtHourRate;
 private JLabel lblGrossPay;
 private JLabel lblNetPay;

  public void init () {
    Container content_pane = getContentPane ();
    content_pane.setLayout (new FlowLayout ());     

        JLabel lblHourWork = new JLabel("Hours Worked");
        JTextField txtHourWork = new JTextField(10);
        JLabel lblHourRate = new JLabel("Pay Rate");
        JTextField txtHourRate = new JTextField(10);


        JButton btnCalculate = new JButton("Calculate Pay");
        btnCalculate.addActionListener(this);

        content_pane.add (lblHourWork);
        content_pane.add(txtHourWork);
        content_pane.add(lblHourRate);
        content_pane.add(txtHourRate);
        content_pane.add(btnCalculate);
        content_pane.add(lblGrossPay);
        content_pane.add(lblNetPay);
        }

 public void actionPerformed (ActionEvent event) {

        String firstNumber = txtHourWork.getText();
        String secondNumber = txtHourRate.getText();

        int number1 = Integer.parseInt(firstNumber);
        int number2 = Integer.parseInt(secondNumber);

        int gross = number1 * number2; 


        double netPay = 0;
        if (gross >= 0 && gross <= 99.99) {
        netPay = gross - gross * .10;
    } else if (gross >= 99.99) {
        netPay = gross - gross * .20;
    }

    lblGrossPay.setText(Integer.toString(gross));
    lblNetPay.setText(Double.toString(netPay));

    }
}

Upvotes: 2

Views: 177

Answers (2)

Reimeus
Reimeus

Reputation: 159754

You never initialise the JLabels lblGrossPay or lblNetPay causing the NPE. You should have:

 lblGrossPay = new JLabel();
 lblNetPay = new JLabel();

Upvotes: 0

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285403

You're shadowing your variables -- redeclaring them in the class's init() method so that all the class JTextFields aren't initialized and thus are null. Solution: Don't do this.

So change it to this:

public void init () {
    Container content_pane = getContentPane ();
    content_pane.setLayout (new FlowLayout ());     

    JLabel lblHourWork = new JLabel("Hours Worked");
    // JTextField txtHourWork = new JTextField(10);
    JLabel lblHourRate = new JLabel("Pay Rate");
    // JTextField txtHourRate = new JTextField(10);

    txtHourWork = new JTextField(10);
    txtHourRate = new JTextField(10);

Upvotes: 3

Related Questions