Need help with error Exception in thread "main" java.lang.Nu

Thread Solved

Join Date: Sep 2007
Posts: 47
Reputation: gator6688 is an unknown quantity at this point 
Solved Threads: 0
gator6688 gator6688 is offline Offline
Light Poster

Need help with error Exception in thread "main" java.lang.Nu

 
0
  #1
May 4th, 2008
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++){
           output += employees[i-1].toString();<<<<<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++ )
         [b]<<<<<This line      JOptionPane.showMessageDialog( null, output );  // display output
      System.exit( 0 );

   } // end main

} // end class PayrollSystemTest

I get two gifferent errors
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help with error Exception in thread "main" java.lang.Nu

 
0
  #2
May 4th, 2008
Originally Posted by gator6688 View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 47
Reputation: gator6688 is an unknown quantity at this point 
Solved Threads: 0
gator6688 gator6688 is offline Offline
Light Poster

Re: Need help with error Exception in thread "main" java.lang.Nu

 
0
  #3
May 4th, 2008
  1. output += employees[i-1].toString();

This is the first error I am working on.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: Need help with error Exception in thread "main" java.lang.Nu

 
0
  #4
May 4th, 2008
Originally Posted by gator6688 View Post
  1. 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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 47
Reputation: gator6688 is an unknown quantity at this point 
Solved Threads: 0
gator6688 gator6688 is offline Offline
Light Poster

Re: Need help with error Exception in thread "main" java.lang.Nu

 
0
  #5
May 4th, 2008
Here is my employee class:

  1. //Employee.java
  2. //Employee abstract superclass.
  3.  
  4. public abstract class Employee
  5. {
  6. private String firstName;
  7. private String lastName;
  8. private String socialSecurityNumber;
  9. private Date birthDate;
  10.  
  11.  
  12. //six-argument constructor
  13. public Employee(String first, String last, String ssn, int month, int day, int year)
  14. {
  15. firstName=first;
  16. lastName=last;
  17. socialSecurityNumber=ssn;
  18. birthDate=new Date(month, day, year);
  19.  
  20. }//end six-argument Employee constructor
  21.  
  22. // set first name
  23. public void setFirstName(String first)
  24. {
  25. firstName=first;
  26. }//end method setFirstName
  27.  
  28. //return first name
  29. public String getFirstName()
  30. {
  31. return firstName;
  32. }//end method getFirstName
  33.  
  34. // set last name
  35. public void setLastName(String last)
  36. {
  37. lastName=last;
  38. }//end method setLastName
  39.  
  40. //return last name
  41. public String getLastName()
  42. {
  43. return lastName;
  44. }//end method getLastName
  45.  
  46. // set social security number
  47. public void setSocialSecurityNumber(String ssn)
  48. {
  49. socialSecurityNumber=ssn; //should validate
  50. }//end method setSocialSecurityNumber
  51.  
  52. //return social security number
  53. public String getSocialSecurityNumber()
  54. {
  55. return socialSecurityNumber;
  56. }//end method getSocialSecurityNumber
  57.  
  58. //set birth date
  59. public void setBirthDate(int month, int day, int year)
  60. {
  61. birthDate=new Date(month, day, year);
  62. }
  63.  
  64. public String getBirthDate()
  65. {
  66. return birthDate.toDateString();
  67. }
  68.  
  69.  
  70.  
  71. //return String representation of Employee object
  72. public String toString()
  73. {
  74. return getFirstName() + " " + getLastName() +
  75. "\nsocial security number: " + getSocialSecurityNumber() +
  76. "\ndate of birth: " + getBirthDate();
  77. }//end method toString
  78.  
  79. //abstract method overridden by subclasses
  80. public abstract double earnings(); // no implementation here
  81. }//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:
  1. if(workerType.equalsIgnoreCase("Salaried")){

Before I ran the debugger I was getting the same error for this line:
  1. output += employees[i-1].toString();

I have tried everything I could think of to fix it but it is not working!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,653
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 223
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Need help with error Exception in thread "main" java.lang.Nu

 
0
  #6
May 5th, 2008
  1. for(int i=1; i<employees.length+1; i++){
  2. 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.
  1. for ( int empNo = 1; empNo <=5; empNo++ ) {
  2. if(workerType.equalsIgnoreCase("Salaried")){ ..
  3. } else if () ... {
  4. } else {
  5. empNo--;
  6. }

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"))
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC