import java.text.DecimalFormat;
import javax.swing.JOptionPane;

public class PayrollSystemTest {

   public static void main( String[] args )
   {
      String workerType;
      String first;
      String last;
      String ssn;
      int month;
      int day;
      int year;
      float salary;
      float rate;
      float hourlyWage=0;
      float hours;  
      float wage;
      float sales;

      DecimalFormat twoDigits = new DecimalFormat( "0.00" );

      // create Employee array
      Employee employees[] = new Employee[ 5 ];

      // generically process each element in array employees
      for ( int empNo = 1; empNo <=5; empNo++ ) {

    
         

    workerType=JOptionPane.showInputDialog(
       "Please enter the type of worker:\n\n" +
       "Salaried\nHourly\nCommission\n" +
       "Base Plus Commission\nQuit, To Quit");

       
       if(workerType.equalsIgnoreCase("Salaried")){
           first=JOptionPane.showInputDialog("Enter employee " + empNo + 
              " first name: ");
        last=JOptionPane.showInputDialog("Enter employee " + empNo +
              " last name: ");
        ssn=JOptionPane.showInputDialog(
            "Enter employee " + empNo + " ssn: ");
        month=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth month(1-12): "));
        day=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth day(1-31): "));
        year=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth year(yyyy): "));
        salary=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " weekly salary: "));
        employees[empNo-1] = new SalariedEmployee(first, last,
              ssn, month, day, year, salary);
       }
           

       else if(workerType.equalsIgnoreCase("Hourly")){
        first=JOptionPane.showInputDialog("Enter employee " + empNo + 
              " first name: ");
        last=JOptionPane.showInputDialog("Enter employee " + empNo +
              " last name: ");
        ssn=JOptionPane.showInputDialog(
            "Enter employee " + empNo + " ssn: ");
        month=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth month(1-12): "));
        day=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth day(1-31): "));
        year=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth year(yyyy): "));
        hourlyWage=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " hourly wage: "));
        hours=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " hours worked: "));
        employees[empNo-1] = new HourlyEmployee(first, last, ssn,
            month, day, year, hourlyWage, hours);
       }
           

       else if(workerType.equalsIgnoreCase("Commission")){
        first=JOptionPane.showInputDialog("Enter employee " + empNo + 
              " first name: ");
        last=JOptionPane.showInputDialog("Enter employee " + empNo +
              " last name: ");
        ssn=JOptionPane.showInputDialog(
            "Enter employee " + empNo + " ssn: ");
        month=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth month(1-12): "));
        day=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth day(1-31): "));
        year=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth year(yyyy): "));
        sales=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " weekly sales: "));
        rate=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " commission rate: "));
        employees[empNo-1] = new CommissionEmployee(first, last,
            ssn, month, day, year, sales, rate);
       }
           
           
       else if(workerType.equalsIgnoreCase("Base Plus Commission")){ 
        first=JOptionPane.showInputDialog("Enter employee " + empNo + 
              " first name: ");
        last=JOptionPane.showInputDialog("Enter employee " + empNo +
              " last name: ");
        ssn=JOptionPane.showInputDialog(
            "Enter employee " + empNo + " ssn: ");
        month=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth month(1-12): "));
        day=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth day(1-31): "));
        year=Integer.parseInt(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " birth year(yyyy): "));
        sales=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " weekly sales: "));
        rate=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " commission rate: "));
        salary=Float.parseFloat(JOptionPane.showInputDialog(
            "Enter employee " + empNo + " base salary: "));
        employees[2] = 
               new BasePlusCommissionEmployee(first, last, ssn, month,
            day, year, sales, rate, salary);
       }

       else if(workerType.equalsIgnoreCase("Quit")){
        break;
       }
           

       
     }


    // initialize array with Employees
      //employees[ 0 ] = new SalariedEmployee( "John", "Smith",
      //   "111-11-1111", 1000, 1, 1, 1960 );
      //employees[ 1 ] = new CommissionEmployee( "Sue", "Jones",
      //   "222-22-2222", 12000, .05, 2, 1, 1962 );
      //employees[ 2 ] = new BasePlusCommissionEmployee( "Bob", "Lewis",
      //   "333-33-3333", 10000, .03, 500, 3, 1, 1963 );
      //employees[ 3 ] = new HourlyEmployee( "Karen", "Price",
      //   "444-44-4444", 20., 40, 4, 1, 1964 );
      //employees[ 4 ] = new SalariedEmployee( "Brian", "Dawkins",
      //   "555-55-5555", 1000, 11, 1, 1961 );

        String output ="";

        for(int i=1; i<employees.length+1; i++){
           [u]output += employees[i-1].toString();[/u][u][b][/b]<<<<<This Line

         // determine whether element is a BasePlusCommissionEmployee
         if ( employees[ i-1 ] instanceof BasePlusCommissionEmployee ) {

            // downcast Employee reference to
            // BasePlusCommissionEmployee reference
            BasePlusCommissionEmployee currentEmployee =
               ( BasePlusCommissionEmployee ) employees[ i-1 ];

            double oldBaseSalary = currentEmployee.getBaseSalary();
            output += "\nold base salary: $" + oldBaseSalary;

            currentEmployee.setBaseSalary( 1.10 * oldBaseSalary );
            output += "\nnew base salary with 10% increase is: $" +
               currentEmployee.getBaseSalary();

         } // end if

         output += "\nearned $" + employees[ i-1 ].earnings()*4 + "\n";

      } // end for

      // get type name of each object in employees array
      for ( int j = 1; j < employees.length+1; j++ )
         [/u][b]<<<<<This line      JOptionPane.showMessageDialog( null, output );  // display output
      System.exit( 0 );

   } // end main

} // end class PayrollSystemTest

I get two gifferent errors

Recommended Answers

All 5 Replies

I get two gifferent errors

What are the errors and where are they? What does the underlined code represent? For code like this, it may be better to wrap your code in Java-specific code tags.

[code=JAVA] // paste code here

[/code]

That'll add line numbers and you can refer to a line number or line numbers to point out where the errors are.

output += employees[i-1].toString();

This is the first error I am working on.

output += employees[i-1].toString();

This is the first error I am working on.

Again, what's the error that Java gave you? You haven't provided the Employee class, so it's hard to answer, but have you written a toString function for the Employee class? If not, Java has no way of knowing what you mean by toString.

Here is my employee class:

//Employee.java
//Employee abstract superclass.

public abstract class Employee
{
    private String firstName;
    private String lastName;
    private String socialSecurityNumber;
    private Date birthDate;
    

    //six-argument constructor
    public Employee(String first, String last, String ssn, int month, int day, int year)
    {
        firstName=first;
        lastName=last;
        socialSecurityNumber=ssn;
	birthDate=new Date(month, day, year);
	
    }//end six-argument Employee constructor

    // set first name
    public void setFirstName(String first)
    {
        firstName=first;
    }//end method setFirstName

    //return first name
    public String getFirstName()
    {
        return firstName;
    }//end method getFirstName

    // set last name
    public void setLastName(String last)
    {
        lastName=last;
    }//end method setLastName

    //return last name
    public String getLastName()
    {
        return lastName;
    }//end method getLastName

    // set social security number
    public void setSocialSecurityNumber(String ssn)
    {
        socialSecurityNumber=ssn; //should validate
    }//end method setSocialSecurityNumber

    //return social security number
    public String getSocialSecurityNumber()
    {
        return socialSecurityNumber;
    }//end method getSocialSecurityNumber

    //set birth date
    public void setBirthDate(int month, int day, int year)
    {
   	birthDate=new Date(month, day, year);
    }

    public String getBirthDate()
    {
	return birthDate.toDateString();
    }

     

    //return String representation of Employee object
    public String toString()
    {
        return getFirstName() + " " + getLastName() + 
	"\nsocial security number: " + getSocialSecurityNumber() + 
	"\ndate of birth: " + getBirthDate(); 
    }//end method toString

    //abstract method overridden by subclasses
    public abstract double earnings(); // no implementation here
}//end abstract class Employee

I got this error when I ran the debugger:
Exception in thread "main" java.lang.NullPointerException
at PayrollSystemTest.main(PayrollSystemTest.java:39

that is this line:

if(workerType.equalsIgnoreCase("Salaried")){

Before I ran the debugger I was getting the same error for this line:

output += employees[i-1].toString();

I have tried everything I could think of to fix it but it is not working!

for(int i=1; i<employees.length+1; i++){
           output += employees[i-1].toString();<<<<<This Line

One of the employees[i-1] is null, meaning that you don't instantiate all the elements of the array. That happens because inside the for-loop you have an option with the command: break; So if the user gives "Quit" you break from the loop and the rest of the elements of the array remain null. Two choices:
Remove the "Quit" option and make sure the user instantiate all the elements. Don't have a break and if a user enters something that is not: "Salaried, Hourly ,Commission, Base Plus Commission" either create a default instance with empty values, or print a message and reduce the counter of the loop in order to male sure that all the elements will have values and that you will not skip one.

for ( int empNo = 1; empNo <=5; empNo++ ) {
 if(workerType.equalsIgnoreCase("Salaried")){ ..
 } else if () ... {
 } else {
    empNo--;
 }

Choice Two:
Have a while-loop: while(true) {...} instead of for-loop. When when user enters"Quit" break from the loop and instead of an array have a Vector or an ArrayList

I think that at the following command:
workerType=JOptionPane.showInputDialog
if you click "Cancel" it returns null. So workerType will be null when you do this:
if (workerType.equalsIgnoreCase("Salaried"))

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.