Click here to Skip to main content
15,741,407 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Whenever i input this so i can Find that Link
System.out.println(theEmployeeList.find("Jason").emName + " Found");
it always shows me this Error
<Exception in thread "main" java.lang.NullPointerException
at Employee.main(Employee.java:38)>

Sorry for bothering you but i really need ur help and my Prof said she wants the removelast and removeinBetween and the ADD to just a Student thankyou for helping me :)

What I have tried:

Java
import java.util.Scanner;
public class Employee
{
	public String emName;
	public int 	  idNumber;
	
	public Employee next;
	
	public Employee(String emName, int idNumber) 
	{
		this.emName = emName;
		this.idNumber = idNumber;
	}
	
	public void display()
	{
		System.out.println(emName + " ID #" + idNumber);
	}
	
	public String toString()
	{
		return emName;
	}
	
	
	public static void main (String [] args)
	{
		String search;
		Scanner sc = new Scanner(System.in);
		EmployeeList theEmployeeList = new EmployeeList();
		
		theEmployeeList.insertFirstEmployee("Jason " , 1);
		theEmployeeList.insertFirstEmployee("Percy " , 2);
		theEmployeeList.insertFirstEmployee("Anabeth " , 3);
		theEmployeeList.insertFirstEmployee("Grover " , 4);
		
		theEmployeeList.display();
		System.out.println(theEmployeeList.find("Jason").emName + " Found");
	}
}
	
class EmployeeList
{
	public Employee firstEmployee;
	
	//First link always starts with Null
	EmployeeList()
	{
		firstEmployee = null;
	}
	
	public boolean isEmpty()
	{
		return(firstEmployee == null);
	}
	
	//Insert 
	public void insertFirstEmployee(String emName, int idNumber)
	{
		Employee newEmployee = new Employee (emName,idNumber);
		newEmployee.next = firstEmployee;
		firstEmployee = newEmployee;
	}
	
	public Employee removeFirst()
	{
		Employee linkReference = firstEmployee; 
		
		if(!isEmpty())
		{
			firstEmployee = firstEmployee.next;
		}   
		else 
		{
			System.out.println("Empty Link List");
		}
		
		return linkReference;	
	}
	
	public void display()
	{
		Employee theEmployee = firstEmployee;
		
		while (theEmployee != null)
		{
			theEmployee.display();
		System.out.println("Next Employee " + theEmployee.next);
		theEmployee = theEmployee.next;
		System.out.println();
		}
	}
	public Employee find(String emName)
	{
		Employee theEmployee = firstEmployee;
		
		if(!isEmpty())
		{
			while(theEmployee.emName != emName)
			{
			
				if(theEmployee.next  == null)
				{
					return null;
				}
				else 
				{
					theEmployee = theEmployee.next;
				}		
			}
		}
		else 
		{
			System.out.println("Empty Link List");
		}
		return theEmployee;
	}
	
	public Employee removeEmployee(String emName)
	{
		Employee currentEmployee = firstEmployee;
		Employee previousEmployee = firstEmployee;
	 
		while(currentEmployee.emName != emName)
		{
			if (currentEmployee.next == null)
			{
				return null; 
			}
			else
		    {
				previousEmployee = currentEmployee; 
				currentEmployee = currentEmployee.next;
			}
		}
		
		if (currentEmployee == firstEmployee)
		{
			firstEmployee = firstEmployee.next;
		} 
		else 
		{
			previousEmployee.next = currentEmployee.next;
		}
	
		return currentEmployee;
	}
	
	
	
	
	

}
Posted
Updated 18-Jan-17 19:21pm

Hi Friend,
You added record with emName as
theEmployeeList.insertFirstEmployee("Jason " , 1);

and trying to find with
System.out.println(theEmployeeList.find("Jason").emName + " Found");

If you note you are missing a blank space after the name it should be like
System.out.println(theEmployeeList.find("Jason ").emName + " Found");
 
Share this answer
 
Comments
Charles Jacob 20-Jan-17 7:32am    
Bro THANKYOU SOMUCH I DIDNT NOTICE THAT HAHAHA
This construct
Java
System.out.println(theEmployeeList.find("Jason").emName + " Found");

is bad because it don't handle a fail of find.
And when it fail,
Java
theEmployeeList.find("Jason").emName

throw the exeption
<Exception in thread "main" java.lang.NullPointerException at Employee.main(Employee.java:38)>
 
Share this answer
 
Comments
Charles Jacob 20-Jan-17 7:32am    
This is correct to ishould go for a Try at catch statement i have space in my Percy thats why i cant find it thankypousomuch for helping

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900