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;
}
}
...全文
96 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
菖蒲老先生 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

至于你提示的错误,我运行的时候没有出现。

58,452

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧