NoSuchMethodError 错误

mxk0921 2012-03-27 11:36:59
用序列化方法写深克隆时出现这个错误。求高手解答

Exception in thread "main" java.lang.NoSuchMethodError: Country.setP(LPopulation;)V
at Client2_3.main(Client2_3.java:14)



import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


public class Client2_3 {
public static void main(String[] args) {
Country america = new Country();
Population p1 = new Population();
p1.setPpl(10000);
america.setP(p1);
int america_ppl = america.getP().getPpl();

Country china = (Country) Country.deepClone(new Country());
china.getP().setPpl(20000);
int china_ppl = china.getP().getPpl();

System.out.println("the population of America is "+america_ppl);
System.out.println("the population of China is "+china_ppl);
}

}

class Population implements Serializable {
private int ppl;

public int getPpl() {
return ppl;
}

public void setPpl(int ppl) {
this.ppl = ppl;
}

}

class Country implements Serializable{
private Population p;

public Population getP() {
return p;
}
public void setP(Population p) {
this.p = p;
}
public static Object deepClone(Object src) {
Object dst = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(src);

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
dst = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dst;
}
}
...全文
53 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
菖蒲芭芭 2012-03-30
哪里不行了,我的运行出结果了。


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Client2_3 {
public static void main(String[] args) {
Country america = new Country();
Population p1 = new Population();
p1.setPpl(10000);
america.setP(p1);
int america_ppl = america.getP().getPpl();

Country china = (Country) Country.deepClone(america);
china.getP().setPpl(20000);
int china_ppl = china.getP().getPpl();

System.out.println("the population of America is " + america_ppl);
System.out.println("the population of China is " + china_ppl);
}

}

class Population implements Serializable {
private int ppl;

public int getPpl() {
return ppl;
}

public void setPpl(int ppl) {
this.ppl = ppl;
}

}

class Country implements Serializable {
private Population p;

public Population getP() {
return p;
}

public void setP(Population p) {
this.p = p;
}

public static Object deepClone(Object src) {
Object dst = null;
try {
ByteArrayOutputStream out = new ByteArrayOutputStream();
ObjectOutputStream oo = new ObjectOutputStream(out);
oo.writeObject(src);

ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
ObjectInputStream oi = new ObjectInputStream(in);
dst = oi.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return dst;
}
}

// 运行结果
// the population of America is 10000
// the population of China is 20000



[Quote=引用 3 楼 的回复:]

谢谢你们的回答。不过我试了还是不行。。
[/Quote]
回复
mxk0921 2012-03-30
我放到另外一个包就没报错。。真TM神奇
回复
mxk0921 2012-03-30
谢谢你们的回答。不过我试了还是不行。。
回复
菖蒲芭芭 2012-03-29
Country china = (Country) Country.deepClone(america);
没你说的那个错。。。
回复
qqhw123 2012-03-29
Country china = (Country) Country.deepClone(new Country());
china.getP().setPpl(20000);
这里有问题,你应该把america放进去,而不是new Country()
不然的话,会报NullPointerException

至于你提示的错误,我运行的时候没有出现。
回复
相关推荐
发帖
Eclipse
创建于2007-09-28

5.8w+

社区成员

Java Eclipse
申请成为版主
帖子事件
创建了帖子
2012-03-27 11:36
社区公告
暂无公告