2016-01-21 69 views
1

我要寫一個可以保存姓名和生日日期的程序,關閉程序後數據不會丟失,如果打開程序,能夠額外用戶添加到用戶根據不幸的是,我總是得到一個計算器。 - 例外,我並沒有發現這個錯誤StackOverFlow - 異常?我找不到原因:-(

<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> 

感謝您的幫助

回答

0

你的問題。這裏

public Eintrag (String name, String gebDatum) 
    { 
     eintrag[i] = new Eintrag (name,gebDatum); 
     ++i; 
    }   

這是一個無盡的l接力。這是一個沒有結束的遞歸調用。 你打電話給它的構造函數自己..所以它這樣做,直到整個堆棧已滿..然後你得到的SF例外:)

+0

思考的人,想:)我所有您的XXX $ /小時,如果你要我做節目:)什麼你需要的是一個列表你將存儲它們的實例。所以也許2班會解決這個更好? – Marty

0

這是一個完全可行的解決方案。這應該讓你開始。祝你好運。

GeburtstagsTool類:

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(); 
} 

Eintrag類:

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 + '\'' + 
      '}'; 
} 
相關問題