我正在尝试将一个对象加载到我从我制作的程序中保存的 Java 中。
加载它时,Java 抛出了 Stack Overflow 错误。
我正在加载的对象ArrayList
里面有大约一千个入口对象,并且在导出时总大小约为 128 Kb。
我知道当我进行太多递归调用时会发生堆栈溢出,但我不明白加载一个对象数组列表如何导致这种情况发生?
我是一名自学成才的程序员,因此任何可以帮助我更好地理解问题的见解都将不胜感激。
这是我要加载的类的代码(Memboric)
public class Memboric implements Serializable{
/**
*
* The Memboric Core stores the various databases the AI needs to function.
*
* @author Brayden McKinney
* @date 5/14/2013
*/
private static final long serialVersionUID = 4477889114653605232L;
ArrayList<String> knownWords = new ArrayList<String>();
HashMap<String, Word> thesaurus = new HashMap<String, Word>();
ArrayList<ConversationLog> conversations = new ArrayList<ConversationLog>();
File location = null;
public static Memboric load(File location){
Memboric memboric;
try{
FileInputStream file = new FileInputStream(location);
ObjectInputStream oo = new ObjectInputStream(file);
memboric = (Memboric) oo.readObject();
oo.close();
System.out.println("Memboric Core loaded and mounted successfully.");
return memboric;
}catch(FileNotFoundException fn){
System.err.println("Failed to load Memboric Core.");
}catch(InvalidClassException ic){
System.err.println("Memboric Core is incompatible.");
}catch(Exception e){
e.printStackTrace();
}
System.err.println("Creating new Memboric Core.");
return new Memboric();
}
public boolean save(File location){
this.location = location;
try{
FileOutputStream file = new FileOutputStream(location);
ObjectOutputStream oo = new ObjectOutputStream(file);
oo.writeObject(this);
oo.close();
System.out.println("Memboric Core saved successfully.");
return true;
}catch(FileNotFoundException ex){
try{
if (location.createNewFile()) save(location);
}catch(IOException exx){
exx.printStackTrace();
}
}catch(Exception e){
e.printStackTrace();
}
return false;
}
它包含的姐妹类(Word):
public class Word implements Serializable{
private static final long serialVersionUID = 790247641945230263L;
private String word;
ArrayList<String> definitions = new ArrayList<String>();
ArrayList<Word> synonyms = new ArrayList<Word>();
ArrayList<String> tags = new ArrayList<String>();
HashSet<String> links = new HashSet<String>();
}
错误本身可以在这里看到(它相当大): http: //pastebin.com/46bFZYVp
此外,上面的代码被删减以节省空间(我删除了不相关的部分),但完整的类可以在这里看到: