Object 堆栈溢出-异常?我能';我没有找到原因:-(
我要写一个可以保存姓名和生日的程序。关闭程序后,数据不会丢失。如果你打开程序,你可以在相应的用户中添加一个额外的用户。不幸的是,我总是遇到stackoverflow-异常,我没有发现错误Object 堆栈溢出-异常?我能';我没有找到原因:-(,object,stream,Object,Stream,我要写一个可以保存姓名和生日的程序。关闭程序后,数据不会丢失。如果你打开程序,你可以在相应的用户中添加一个额外的用户。不幸的是,我总是遇到stackoverflow-异常,我没有发现错误 <pre> package geburtstagstool; import java.io.*; public class GeburtstagsTool { public static void main (String[]args) throws Exception {
<pre> package geburtstagstool;
import java.io.*;
public class GeburtstagsTool
{
public static void main (String[]args) throws Exception
{
Eintrag eintrag = new Eintrag ("Miller","000000");
}
}
class Eintrag implements Serializable
{
Eintrag [] eintrag = new Eintrag [50];
public String name;
public String gebDatum;
int i=0;
public Eintrag (String name, String gebDatum)
{
eintrag[i] = new Eintrag (name,gebDatum);
++i;
}
public void testSchreiben () throws Exception
{
ObjectOutputStream oos = new ObjectOutputStream (new FileOutputStream ("eintrag.dat"));
oos.writeObject(eintrag);
oos.close();
}
public static Eintrag testLesen() throws IOException, ClassNotFoundException
{
ObjectInputStream ois = new ObjectInputStream (new FileInputStream ("eintrag.dat"));
Eintrag eint = (Eintrag) ois.readObject();
ois.close();
return eint;
}
}
<code>
谢谢你的帮助。你的问题就在这里
public class GeburtstagsTool {
List<Eintrag> eintragList;
public static void main (String[] args) throws IOException, ClassNotFoundException
{
GeburtstagsTool geburtstagsTool = new GeburtstagsTool();
geburtstagsTool.loadEintrag();
System.out.println("What's already in the list: \n" + geburtstagsTool.eintragList);
Eintrag eintrag = new Eintrag("Peyton Manning", "03/24/1976");
geburtstagsTool.eintragList.add(eintrag);
geburtstagsTool.writeEintrag();
}
public void loadEintrag() {}
{
ObjectInputStream ois = null;
try
{
ois = new ObjectInputStream(new FileInputStream("eintrag.dat"));
eintragList = (List<Eintrag>) ois.readObject();
}
catch (IOException e)
{
System.out.println("File doesn't exist");
eintragList = new ArrayList<>();
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}
public void writeEintrag() throws IOException
{
ObjectOutputStream oos = null;
try
{
oos = new ObjectOutputStream(new FileOutputStream("eintrag.dat"));
}
catch (IOException e)
{
e.printStackTrace();
}
oos.writeObject(eintragList);
oos.close();
}
这是一个无止境的循环,是一个无止境的递归调用。
你调用调用它自身的构造函数。它会这样做,直到整个堆栈都满了。然后你会得到SF异常:)这是一个完全有效的解决方案。这应该让你开始。祝你好运 GeburtstagsTool类:
public class Eintrag implements Serializable{
String name;
String gebDatum;
public Eintrag(String name, String gebDatum) {
this.name = name;
this.gebDatum = gebDatum;
}
@Override
public String toString()
{
return "Eintrag{" +
"name='" + name + '\'' +
", gebDatum='" + gebDatum + '\'' +
'}';
}
想一想,想一想:)如果你想让我做编程,我愿意为你提供xxx$/小时:)你需要的是一个列表,你可以在其中存储它们的实例。那么也许两个类可以更好地解决这个问题?
public class Eintrag implements Serializable{
String name;
String gebDatum;
public Eintrag(String name, String gebDatum) {
this.name = name;
this.gebDatum = gebDatum;
}
@Override
public String toString()
{
return "Eintrag{" +
"name='" + name + '\'' +
", gebDatum='" + gebDatum + '\'' +
'}';
}