Hello I wrote a class Employee and a tester class TestEmployee.The problem is that I get this exception after I enter the first employee's id.I thght if I give default values to the instance field the exception shouldn't have occured; can someone help me with this please:
Employee class:
/**A employee can be identified using the employee's id
* and has a first name and a last name
* @author User
*
*/
public class Employee {
private int eid = 0;
private String firstName = "Default String";
private String lastName = "Default String";
/**gets the employee's id
* @return employee's id
*/
public int getEid()
{
return eid;
}
/**gets the employee's first name
* @return employee's first name
*/
public String getFirstName()
{
return firstName;
}
/**gets the employee's last name
* @return employee's last name
*/
public String getLastName()
{
return lastName;
}
/**sets the employee's id
* @param patient's eid
*/
public void setEid( int eid )
{
//this refers to the implicit parameter
//that is the object on which the method
//is being invoked
this.eid = eid;
}
/**sets the employee's first name
* @param employee's first name
*/
public void setFirstName( String firstName )
{
this.firstName = firstName;
}
/**sets the employee's last name
* @param employee's last name
*/
public void setLastName( String lastName )
{
this.lastName = lastName;
}
}
Class TestEmployee:
import java.util.Scanner;
public class TestEmployee {
public static void main( String [] args )
{
Scanner in = new Scanner( System.in );
//[] on the L.H.S indicates that the variable
//stores a reference to an array of Employee objects
Employee[] empArr = new Employee[5];
//use the variable to access the array of Employee objects
int i, j;
for( i = 0; i < empArr.length; i++ )
{
j = i + 1;
System.out.print( "Please enter employee-" + j + " id:");
empArr[i].setEid( in.nextInt());
System.out.print( "Please enter employe-" + j + " first name:");
empArr[i].setFirstName( in.nextLine());
System.out.print( "Please enter employe-" + j + " last name:");
empArr[i].setLastName( in.nextLine());
}
for( i = 0; i < empArr.length; i++ )
{
System.out.println( "Employee Id: " + empArr[i].getEid() );
System.out.println( "Employee First Name: " + empArr[i].getFirstName());
System.out.println( "Employee Last Name: " + empArr[i].getLastName());
}
}
}