为什么会出现Exception in thread "main" java.lang.NullPointerException 的错误? 5
packagepac;importjava.util.*;publicclassCloneTest{publicstaticvoidmain(String[]args){...
package pac;
import java.util.*;
public class CloneTest {
public static void main(String[] args) {
try {
Employee original = new Employee("zhang san", 3000);
original.setHireDay(2000, 1, 1);
Employee clone = original.clone();
clone.raiseSalary(10);
clone.setHireDay(2001, 2, 24);
System.out.println("original:" + original);
System.out.println("clone:" + clone);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
class Employee implements Cloneable {
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public Date setHireDay(int year, int month, int day) {
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
Date date = calendar.getTime();
return date;
}
public double raiseSalary(double d){
return salary+salary*d/100;
}
public String toString(){
return "name:"+name+"salary:"+salary+"hireDay"+hireDate;
}
protected Employee clone()throws CloneNotSupportedException{
//call Object.clone();
Employee cloned=(Employee)super.clone();
//clone mutable fields
cloned.hireDate=(Date)hireDate.clone();
return cloned;
}
private String name = "";
private double salary = 0;
private Date hireDate;
} 展开
import java.util.*;
public class CloneTest {
public static void main(String[] args) {
try {
Employee original = new Employee("zhang san", 3000);
original.setHireDay(2000, 1, 1);
Employee clone = original.clone();
clone.raiseSalary(10);
clone.setHireDay(2001, 2, 24);
System.out.println("original:" + original);
System.out.println("clone:" + clone);
} catch (CloneNotSupportedException e) {
e.printStackTrace();
}
}
}
class Employee implements Cloneable {
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public Date setHireDay(int year, int month, int day) {
GregorianCalendar calendar = new GregorianCalendar(year, month - 1, day);
Date date = calendar.getTime();
return date;
}
public double raiseSalary(double d){
return salary+salary*d/100;
}
public String toString(){
return "name:"+name+"salary:"+salary+"hireDay"+hireDate;
}
protected Employee clone()throws CloneNotSupportedException{
//call Object.clone();
Employee cloned=(Employee)super.clone();
//clone mutable fields
cloned.hireDate=(Date)hireDate.clone();
return cloned;
}
private String name = "";
private double salary = 0;
private Date hireDate;
} 展开
5个回答
展开全部
cloned.hireDate=(Date)hireDate.clone();
这句话里面的hireDate你就没有初始化,当然会抛出空指针异常啦。
你setHireDay()里面有初始化你在最后定义的private Date hireDate;吗????
这句话里面的hireDate你就没有初始化,当然会抛出空指针异常啦。
你setHireDay()里面有初始化你在最后定义的private Date hireDate;吗????
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
重写后clone的方法签名是 public Object clone() throws CloneNotSupportedException{..}
另外 NPE 一般是没有初始化变量,你在出错的哪里看看哪个变量值是null。
另外 NPE 一般是没有初始化变量,你在出错的哪里看看哪个变量值是null。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
空指针是因为hireDate没有值,
cloned.hireDate=(Date)hireDate.clone();会抛异常
cloned.hireDate=(Date)hireDate.clone();会抛异常
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
hireDate没有初始值
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
一般包空指针的异常 你找到报错那一行, 一般都是.前面的东西为空。倒着查一下就能看到哪里为空了 。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询