关于空指针的问题,望高人解答【急】
没有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();
company.removeEmployee(number);
break;
default:
if(!answer.equals("quit"))
System.out.println("****Error! The value is invalid, try again.****\n");
}
}while(!answer.equals("quit"));
}
}
[code=Java][/code]import java.util.ArrayList;
public class Company
{
public Company(String newCompanyName)
{
companyName = newCompanyName;
ArrayList<Employee> employees = new ArrayList<Employee>();
}
public void addEmployee(Employee emp)
{
employees.add(emp);
}
public String findEmployee(int employeeNumber)
{
for(Employee emp : employees)
{
if(emp.getEmployeeNumber() == employeeNumber)
return emp.toString();
}
return "No match.";
}
public String findHighest()
{
int highestInd = 0;
if(employees.size() == 0) return null;
for(int ind=0; ind < employees.size() - 1; ind++ )
{
if((employees.get(ind)).getSalary() < (employees.get(ind+1)).getSalary())
{
highestInd = ind + 1;
}
else
{
highestInd = ind;
}
ind = ind++;
}
return (employees.get(highestInd)).toString();
}
public ArrayList<Employee> getList()
{
return employees;
}
public String removeEmployee(int employeeNumber)
{
for(int ind=0; ind < employees.size(); ind++)
{
if(employeeNumber == (employees.get(ind)).getEmployeeNumber())
{
employees.remove(ind);
return ((employees.get(ind)).getName() + " is deleted.");
}
}
return "No match.";
}
private String companyName;
private ArrayList<Employee> employees;
}
[code=Java][/code]/**
* CPSC 1181 Assignment #2
* Instructor: Hengameh Hamavand
* <p>
* I pledge that I have completed the programming assignment independently.
* I have not copied the code from a student or any other source.
* I have not given my code to any student.
* Siny (Xiao Yang Ma), May. 21. 2012
* <p>
* This program is use for creating employee account.
* User can read, change and display information form this program.
* <p>
* @author Siny (Ma, Xiao Yang) 100182585
* @version 1
*/
public class Employee
{
/**
* Employee method: read from other method.
* read name. employee number, address, salary and bonus of a employee.
* <p>
* return Employee.
* @param newName string show a few line entered by user.
* @param newAddress string show a few line entered by user.
* @param newEmployeeNumber int show a number entered by user.
* @param newSalary double show a number entered by user.
* @param newBonus double array show a array entered by user.
* <p>
* precondition: the input string name and address, double salary, int empolyee number and double array bonus separate by a comma.
*/
public Employee(String newName, int newEmployeeNumber, String newAddress,int newSalary, int[] newBonus)
{
name = newName;
employeeNumber = newEmployeeNumber;
address = newAddress;
salary = newSalary;
bonus = newBonus;
}
/**
* getName method.
* <p>
* return string.
* <p>
* precondition: return name string to the method which is called it.
*/
public String getName()
{
return name;
}
/**
* getEmployeeNumber.
* <p>
* return double.
* <p>
* precondition:return employeeNumber double to the method which is called it.
*/
public double getEmployeeNumber()
{
return employeeNumber;
}
/**
* getAddress method.
* <p>
* return String.
* <p>
* precondition:return address string to the method which is called it.
*/
public String getAddress()
{
return address;
}
/**
* getSalary method.
* <p>
* return double.
* <p>
* precondition:return salary double to the method which is called it.
*/
public double getSalary()
{
return salary;
}
/**
* getBonus method.
* <p>
* return double array.
* <p>
* precondition:return bonus double array to the method which is called it.
*/
public int[] getBonus()
{
return bonus;
}
/**
* getIndexOfBonus method.
* <p>
* return int.
* <p>
* precondition:return indexOfBonus int to the method which is called it.
*/
public int getIndexOfBonus()
{
return indexOfBonus;
}
/**
* changeAddress method.
* <p>
* void method not return any variable.
* <p>
* precondition: the string new_address.
*/
public void changeAddress(String new_address)
{
address = new_address;
}
/**
* changeSalary method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double new_salary.
*/
public void changeSalary(int new_salary)
{
salary = new_salary;
}
/**
* changeBonus method.
* <p>
* void method not return any variable.
* <p>
* precondition: the double array new_bonus.
*/
public void addBonus(int bonuses)
{
bonus[indexOfBonus] = bonuses;
indexOfBonus++;
}
/**
* changeIndexOfBonus method.
* <p>
* void method not return any variable.
* <p>
* precondition: the int new_indexOfBonus.
*/
public void changeIndexOfBonus(int new_indexOfBonus)
{
indexOfBonus = new_indexOfBonus;
}
/**
* toString method.
* <p>
* return string.
* <p>
* postcondition: return a few line string to the method which is called it.
*/
public String toString()
{
String bonusString = "";
for(int ind=0; ind < indexOfBonus; ind++)
{
bonusString = bonusString + bonus[indexOfBonus];
}
return ("Name: "+name+"\nEmployee Number: "+employeeNumber+"\nAddress: "+address+"\nSalary: "+salary+"\nList of bonus:\n"+bonusString);
}
//define private variables
private String name;
private int employeeNumber;
private String address;
private int salary;
private int[] bonus;
private int indexOfBonus = 0;
}
[解决办法]
空指针,debug一下就出来了
public Company(String newCompanyName)
{
companyName = newCompanyName;
ArrayList<Employee> employees = new ArrayList<Employee>();
}