本文介绍了帮我链接列表sinlgly的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我输入这个,所以我可以找到链接

Whenever i input this so i can Find that Link

System.out.println(theEmployeeList.find("Jason").emName + " Found");

它总是向我显示此错误

<线程中的异常mainjava.lang.NullPointerException

at Employee.main(Employee.java:38)>



很抱歉打扰你,但我真的需要你的帮助,我的教授说她想要 removelast removeinBetween ADD到只是一个学生谢谢你帮助我:)



我尝试过:



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:

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

}

推荐答案


System.out.println(theEmployeeList.find("Jason").emName + " Found");



不好,因为它无法处理的失败

当它失败时,


is bad because it don't handle a fail of find.
And when it fail,

theEmployeeList.find("Jason").emName



抛出这个例子


throw the exeption

<Exception in thread "main" java.lang.NullPointerException at Employee.main(Employee.java:38)>


这篇关于帮我链接列表sinlgly的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 15:31