当前位置: 代码迷 >> Java相关 >> 关于空指针的有关问题,望高人解答
  详细解决方案

关于空指针的有关问题,望高人解答

热度:6229   发布时间:2013-02-25 21:45:12.0
关于空指针的问题,望高人解答【急】
没有syntax error,但是却出现了semantic error。麻烦高人指点错误出错之处还有改善方法,十分感谢!!!
以下是运行后出来的错误:
>java -cp . CompanyTest
Please enter a company name
tom
Company directory commands
add - Add a new Employee
find - Find an Employee
addBonus - Add a bonus
findHighest - Find an employee with highest salary
delete - Delete an Employee
quit - Quit

add
Please enter a name.
aqw
Please enter an employee number.
1234
Please enter the salary.
1111
Please enter the address.
aaaa
Exception in thread "main" java.lang.NullPointerException
at Company.addEmployee(Company.java:12)
at CompanyTest.run(CompanyTest.java:55)
at CompanyTest.main(CompanyTest.java:11)
>Exit code: 1
到这里结束。
下面的是我所需要的全部的class(共三个)。

[code=Java][/code]import java.util.*;
public class CompanyTest
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Company company;

company = read_input(in);

run(company, in);
}

public static Company read_input(Scanner in)
{
String companyName;
System.out.println("Please enter a company name");
companyName = in.nextLine();
return new Company(companyName);
}

public static void run(Company company, Scanner in )
{
String answer, name, address, answer_1;
int number, employeeNumber, salary, bonuses;
Employee emp1;
int[] bonus = null;

do
{
System.out.println("Company directory commands");
System.out.println("add - Add a new Employee");
System.out.println("find - Find an Employee");
System.out.println("addBonus - Add a bonus");
System.out.println("findHighest - Find an employee with highest salary");
System.out.println("delete - Delete an Employee");
System.out.println("quit - Quit");
System.out.println();

answer = in.next();

switch(answer)
{
case "add":
System.out.println("Please enter a name.");
name = in.next();
System.out.println("Please enter an employee number.");
employeeNumber = in.nextInt();
System.out.println("Please enter the salary.");
salary = in.nextInt();
System.out.println("Please enter the address.");
address = in.next();

emp1 = new Employee(name, employeeNumber, address, salary, bonus);
company.addEmployee(emp1);
break;
case "find":
System.out.println("Please enter an employee number which you are going to find.");
number = in.nextInt();
company.findEmployee(number);
break;
case "addBonus":
System.out.println("Please enter an employee number which you are going to add.");
number = in.nextInt();
for(Employee emp: company.getList())
if(emp.getEmployeeNumber() == number)
do
{
System.out.println("Please enter a bonus");
bonuses = in.nextInt();
emp.addBonus(bonuses);
System.out.println("Do you want to continue?(press \" yes \" to go, others to leave)");
answer_1 = in.next();
}while(answer_1.equals("yes"));
else 
System.out.println("No match.");
break;
case "findHighest":
System.out.println("the employee who has the highest salary: \n"+company.findHighest());
break;
case "delete":
System.out.println("Please enter an employee number which you are going to delete.");
number = in.nextInt();
  相关解决方案