0

So I have a school assignment to do in Java, I have created the program but it does not give me an output. Im still a beginner so plz help me fix the problem. the first half of my code uses joptionpane but i want to display the output in the console. this is my code:

import java.util.Scanner;
import javax.swing.JOptionPane;

public class payroll2
{
    public static void main(String args[])
    {
        new payroll2().SetPayrollDetail();
        new payroll2().SetBonus();
        new payroll2().SetCommission();
        new payroll2().SetNssf();
        new payroll2().SetNetSalary();
        new payroll2().GetPayroll();
    }

    Scanner myScanner=new Scanner(System.in);
    Double empID;
    String empName;
    String empDept;
    String designation;
    Double basicSalary;

    Double bonus;
    Double commission;
    Double nssf;
    Double netSalary;


    public void SetPayrollDetail()
    {
        empID = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter ID: ", "ID",JOptionPane.QUESTION_MESSAGE));

        empName = JOptionPane.showInputDialog(null, "Enter Name: ", "Name",JOptionPane.QUESTION_MESSAGE);

        empDept = JOptionPane.showInputDialog(null, "Enter Department (Marketing or Other): ", "Department",JOptionPane.QUESTION_MESSAGE);

        designation = JOptionPane.showInputDialog(null, "Enter Designation (Manager, Executive or Other): ", "Designation",JOptionPane.QUESTION_MESSAGE);

        basicSalary = Double.parseDouble(JOptionPane.showInputDialog(null, "Enter Basic Salary: ", "Basic Salary",JOptionPane.QUESTION_MESSAGE));
    }

    public void SetBonus()
    {
        if(basicSalary < 1500){
            bonus = 0.0;
        }
        else if(basicSalary>=1500 && basicSalary<3000){
            bonus = basicSalary * (12/100);
        }
        else if(basicSalary>=3000 && basicSalary<5000){
            bonus = basicSalary * (15/100);
        }
        else{
            bonus = basicSalary * (25/100);
        }

        System.out.println(bonus);
    }

    public void SetCommission()
    {
        if( empDept.equalsIgnoreCase("other") ){
            commission = 0.0;
        }
        else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("manager") ){
            commission = basicSalary * (30/100);
        }
        else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("executive") ){
                    commission = basicSalary * (15/100);
        }
        else if( empDept.equalsIgnoreCase("marketing") && designation.equalsIgnoreCase("other") ){
                    commission = basicSalary * (10/100);
        }
        else{
            commission = 0.0;
        }

        System.out.println(commission);
    }

    public void SetNssf()
    {
        if(basicSalary < 1500){
            nssf = basicSalary * (5/100);
        }
        else if(basicSalary>=1500 && basicSalary<3000){
            nssf = basicSalary * (8/100);
        }
        else if(basicSalary>=3000 && basicSalary<5000){
            nssf = basicSalary * (12/100);
        }
        else if(basicSalary>=5000 && basicSalary<7000){
            nssf = basicSalary * (15/100);
        }
        else if(basicSalary>=7000 && basicSalary<10000){
            nssf = basicSalary * (20/100);
        }
        else{
            nssf = basicSalary * (25/100);
        }

        System.out.println(nssf);
    }

    public void SetNetSalary()
    {
        netSalary = ( basicSalary + commission + bonus ) - nssf;

        System.out.println(netSalary);
    }

    public void GetPayroll()
    {
        System.out.println("Payroll Details \n _____________________");
        System.out.println("ID:\t" + empID);
        System.out.println("name:\t" + empName);

        System.out.println(bonus);
        System.out.println(commission);
        System.out.println(nssf);
        System.out.println(netSalary);

    }

}
  • 3
    What does the stacktrace tell you? – Reimeus Oct 15 '14 at 18:40
  • Where is the stacktrace? Also if you want to use just Console, do not use Swing widgets like `JOptionPane`. You might want look into `BufferedReader` and `InputStreamReader` classes. – toddlermenot Oct 15 '14 at 18:41
  • 4
    Note: every statement in your main() creates a new payroll2 object, calls a method on it, and then *throws it away*. The getPayroll() method is being called on an object created with the default constructor - which initialized all your fields of reference type to `null`. – Andy Thomas Oct 15 '14 at 18:41
  • 2
    NullPointerException is just about the easiest of all Java errors to debug. Look at the stack trace and figure out where the error is coming from. (Including the *exact* exception message *and* the stack trace when seeking help for an exception is *required*.) – Hot Licks Oct 15 '14 at 18:42

2 Answers2

3

You have a bunch of values which aren't initialized to anything by default:

Double empID;
String empName;
String empDept;
// etc.

When you create an instance of the object, you call a method to initialize them:

new payroll2().SetPayrollDetail();

But then you don't keep that instance. You immediately create an entirely new instance and try to operate on it:

new payroll2().SetBonus();

This new instance never had its values initialized to anything. So it can't use those values. Instead, you want to keep that original instance so you can operate on that:

payroll2 instance = new payroll2();
instance.SetPayrollDetail();
instance.SetBonus();
// etc.
David
  • 208,112
  • 36
  • 198
  • 279
0

You are recreating your instance of payroll2 every time you make an operation. Use a single instance.

payroll2 p = new payroll2();
p.SetPayrollDetail();
p.SetBonus();
p.SetCommission();
p.SetNssf();
p.SetNetSalary();
p.GetPayroll();

Moreover, method should begin with a tiny letter, class name with a capital one.

ToYonos
  • 16,469
  • 2
  • 54
  • 70