Java运行错误:Exception in thread "main" java.lang.NullPointerException
Eclipse中运行错误功能:建立Employee类(姓名,年薪,受雇时间),MyDate类(年月日),测试类代码:packageabc.cba;classMyDate{...
Eclipse中运行错误
功能:建立Employee类(姓名,年薪,受雇时间),MyDate类(年月日),测试类
代码:
package abc.cba;
class MyDate {
private int day;
private int month;
private int year;
public void setDay (int day) {this.day = day;}
public void setMonth (int month) {this.month = month;}
public void setYear (int year) {this.year = year;}
public int getDay () {return this.day;}
public int getMonth () {return this.month;}
public int getYear () {return this.year;}
}
class Employee {
private String name;
private double annualSalary;
private MyDate employeedDate;
public void setName (String name) {this.name = name;}
public void setAnnualSalary (double annualSalary) {this.annualSalary = annualSalary;}
public void setEmployeedDate (int year_1, int month_1, int day_1) {
employeedDate.setYear(year_1);
employeedDate.setMonth(month_1);
employeedDate.setDay(day_1);
}
public String getName () {return this.name;}
public double getAnnualSalary () {return this.annualSalary;}
void tellAll () {
System.out.println("雇员姓名:" + name);
System.out.println("雇员年薪:" + annualSalary);
System.out.println("受雇时间:"+employeedDate.getYear()+"年"+employeedDate.getMonth()+"月"+employeedDate.getDay()+"日");
}
}
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp1 = new Employee ();
emp1.setName("小明");
emp1.setAnnualSalary(100000);
emp1.setEmployeedDate(2012, 2, 1);
emp1.tellAll();
}
}
运行错误:
Exception in thread "main" java.lang.NullPointerException
at abc.cba.Employee.setEmployeedDate(TestEmployee.java:24)
at abc.cba.TestEmployee.main(TestEmployee.java:50)
求解。 展开
功能:建立Employee类(姓名,年薪,受雇时间),MyDate类(年月日),测试类
代码:
package abc.cba;
class MyDate {
private int day;
private int month;
private int year;
public void setDay (int day) {this.day = day;}
public void setMonth (int month) {this.month = month;}
public void setYear (int year) {this.year = year;}
public int getDay () {return this.day;}
public int getMonth () {return this.month;}
public int getYear () {return this.year;}
}
class Employee {
private String name;
private double annualSalary;
private MyDate employeedDate;
public void setName (String name) {this.name = name;}
public void setAnnualSalary (double annualSalary) {this.annualSalary = annualSalary;}
public void setEmployeedDate (int year_1, int month_1, int day_1) {
employeedDate.setYear(year_1);
employeedDate.setMonth(month_1);
employeedDate.setDay(day_1);
}
public String getName () {return this.name;}
public double getAnnualSalary () {return this.annualSalary;}
void tellAll () {
System.out.println("雇员姓名:" + name);
System.out.println("雇员年薪:" + annualSalary);
System.out.println("受雇时间:"+employeedDate.getYear()+"年"+employeedDate.getMonth()+"月"+employeedDate.getDay()+"日");
}
}
public class TestEmployee {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Employee emp1 = new Employee ();
emp1.setName("小明");
emp1.setAnnualSalary(100000);
emp1.setEmployeedDate(2012, 2, 1);
emp1.tellAll();
}
}
运行错误:
Exception in thread "main" java.lang.NullPointerException
at abc.cba.Employee.setEmployeedDate(TestEmployee.java:24)
at abc.cba.TestEmployee.main(TestEmployee.java:50)
求解。 展开
4个回答
展开全部
private MyDate employeedDate没有实例化,应该private MyDate employeedDate=new MyDate();
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
空指针 setEmployeedDate 这个方法中的 employeedDate对象为空 没有初始化
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
EmployeedDate是对象,应该使用new EmployeedDate来创建类实例,然后给它的成员变量赋值。
在Employee的构造函数中,初始化该对象,或在变量声明时初始化该对象。如:
private MyDate employeedDate = new EmployeedDate();
在Employee的构造函数中,初始化该对象,或在变量声明时初始化该对象。如:
private MyDate employeedDate = new EmployeedDate();
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询