| | |
Need help with error Exception in thread "main" java.lang.Nu
Thread Solved |
•
•
Join Date: Sep 2007
Posts: 47
Reputation:
Solved Threads: 0
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 PayrollSystemTestI get two gifferent errors
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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.
[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.
•
•
Join Date: Sep 2007
Posts: 47
Reputation:
Solved Threads: 0
Java Syntax (Toggle Plain Text)
output += employees[i-1].toString();
This is the first error I am working on.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
•
•
•
•
Java Syntax (Toggle Plain Text)
output += employees[i-1].toString();
This is the first error I am working on.
•
•
Join Date: Sep 2007
Posts: 47
Reputation:
Solved Threads: 0
Here is my employee class:
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:
Before I ran the debugger I was getting the same error for this line:
I have tried everything I could think of to fix it but it is not working!
Java Syntax (Toggle Plain Text)
//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:
Java Syntax (Toggle Plain Text)
if(workerType.equalsIgnoreCase("Salaried")){
Before I ran the debugger I was getting the same error for this line:
Java Syntax (Toggle Plain Text)
output += employees[i-1].toString();
I have tried everything I could think of to fix it but it is not working!
Java Syntax (Toggle Plain Text)
for(int i=1; i<employees.length+1; i++){ output += employees[i-1].toString();<<<<<This Line
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.
Java Syntax (Toggle Plain Text)
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"))
Check out my New Bike at my Public Profile at the "About Me" tab
![]() |
Similar Threads
- Exception in thread "main" java.lang.NoClassDefFoundError: Invaders Error (Java)
- help in Exception (Exception in thread "AWT-EventQueue-0") (Java)
- error in "main" (Java)
- my program is compiled.but on run that i have message "Exception in thread "main" jav (Java)
- exception in thread "main" java.lang.NoClassDefFoundError (Java)
Other Threads in the Java Forum
- Previous Thread: JAXB
- Next Thread: MVC - Model View Controller
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working






