马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 dlnb526 于 2020-2-6 22:55 编辑 ///CoreJAVA 4-2
import java.time.*;
public class EmployeeTest
{
public static void main(String[] args)
{
Employee[] staff = new Employee[3];
staff[0] = new Employee("Carl Cracker",75000,1987,12,15);
staff[1] = new Employee("Harry Havker",50000,1989,10,1);
for(Employee e:staff)
e.raiseSalary(5);
for(Employee e:staff)
System.out.println("name="+e.getName()+",salary="+e.getSalary()+",hireDay="+e.getHireDay());
}
}
class Employee
{
private String name;
private double salary;
private LocalDate hireDay;
public Employee(String n,double s,int year,int month,int day)
{
name = n;
salary = s;
hireDay = LocalDate.of(year, month, day);
}
public String getName()
{
return name;
}
public double getSalary()
{
return salary;
}
public LocalDate getHireDay()
{
return hireDay;
}
public void raiseSalary(double byPercent)
{
double raise = salary*byPercent/100;
salary += raise;
}
}
错误如下Exception in thread "main" java.lang.NullPointerException
at EmployeeTest.main(EmployeeTest.java:15)
就是这里: for(Employee e:staff)
e.raiseSalary(5);
可是没有需要赋值的地方了啊~~求各位大佬帮助 |