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());
		}
		
	}
	

}

Recommended Answers

All 2 Replies

Employee[] empArr = new Employee[5];
...
for( i = 0; i < empArr.length; i++ )
...
empArr[i].setEid( in.nextInt());

You create an array of 5 elements that can be used to hold Employees, but this does not populate the array with actual Employees. So when you try to use empArr[0] there's nothing there and you get a null pointer exception.
A quick empArr = new Employee(); at the start of the loop will fix it, but the more usual approach is to write a constructor that takes all the mandatory values so you can create an Employee with something like
new Employee(eid, firstName, lastName)

LOL i frgt that line:
empArr = new Employee();

Thanks for helping :)

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.