Help with Error Exception in thread "main" java.lang.NullPointerException

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gator6688
    New Member
    • Sep 2007
    • 63

    Help with Error Exception in thread "main" java.lang.NullPointerException

    Code:
    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;
          float hours;  
          
          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");
    
    	   
    	   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);
    	   }
    
    	   
    	   
         }
    
    
    	// 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( "Bruce", "Chiesa",
          //   "555-55-5555", 1000, 11, 1, 1961 );
    
    		String output ="";
    
    		for(int i=1; i<employees.length+1; i++){
    		   output += employees[i-1].toString();
    
             // 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++ )
             output += "\nEmployee " + j + " is a " +
                employees[ j-1 ].getClass().getName();
    
          JOptionPane.showMessageDialog( null, output );  // display output
          System.exit( 0 );
    
       } // end main
    
    } // end class PayrollSystemTest
    error is on this line on 148
    Code:
    output += employees[i-1].toString();
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    i goes from 1 to employees.lengt h+1, so i-1 goes from 0->employees.leng th. However, because arrays are zero-indexed, employees[employees.lengt h] is the employees.lengt h+1'th item. Thus, you're outside your array. Quite frankly, I'm surprised you got a NullPointerExce ption, as you should probably have gotten an ArrayIndexOutOf BoundsException first when it tries to resolve the element.

    Comment

    • gator6688
      New Member
      • Sep 2007
      • 63

      #3
      How do I fix it? Do I make i=0?

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by gator6688
        How do I fix it? Do I make i=0?
        See if there are actual employees in your array:

        [code=java]
        for (int i= 0; i < employees.lengt h; i++)
        System.out.prin tln(i+": "+employees[i]);
        [/code]

        If there is an employee present at slot i something will be printed, otherwise just
        'null' will be printed. You attempt to override the toString() methond so something
        sensible should be printed if an employee is printed.

        Als note how the little loop above iterates over the array, i.e. there is no need to
        start from 1 and subtract one from the index value again in the body of the loop.

        kind regards,

        Jos

        Comment

        Working...