慕婉清6462132
2022-07-06 16:51:12
我的第一個代碼塊是我的項目對象文件;第二個是主類。在代碼運行沒有任何問題之前,但在我添加了讀寫文件之后,我的代碼開始收到堆棧流錯誤。只是調用錯誤的片段。 public class Item implements java.io.Serializable { public static String name; public static double price; public static double amount; public int max = 1; SlayerProgram sp = new SlayerProgram(); ReadFile rf = new ReadFile(); public Item(String name, double price,double amount ) { this.name = name; this.price = price; this.amount = amount; } public void ItemSet(String name, double price,double amount) { this.name = name; this.price = price; this.amount = amount }我的主要課程: public class SlayerProgram {//import file txts, and Item Class static String name; static double price; static double amount; Item item = new Item(name,price,amount);ReadFile rf = new ReadFile();static String fileNameText = "D:\\Game\\SlayerProgram\\Name.txt";static String filePriceInt = "D:\\Game\\SlayerProgram\\Price.txt";static String fileAmountInt ="D:\\Game\\SlayerProgram\\Amount.txt"; //begin file Read public void BeginText() throws IOException{ TextFile();}public void Max(){ item.Max();} //declare needed Data Types; final int max = item.max; ArrayList<String> Name = new ArrayList<>(); ArrayList<Double> Price = new ArrayList<>(); double size = Price.size(); ArrayList<Double> Amount = new ArrayList<>();Exception in thread "main" java.lang.StackOverflowErrorat slayerprogram.Item.<init>(Item.java:18)at slayerprogram.SlayerProgram.<init>(SlayerProgram.java:25)at slayerprogram.Item.<init>(Item.java:18)at slayerprogram.SlayerProgram.<init>(SlayerProgram.java:25)如何找到導致堆棧溢出的位置?
2 回答

繁華開滿天機
TA貢獻1816條經驗 獲得超4個贊
Item
創建SlayerProgram
:
SlayerProgram sp = new SlayerProgram();
并SlayerProgram
創造Item
Item item = new Item(name,price,amount);
因此,在初始化時,您將無休止地創建這些對象
有一個類似的Baeldung 示例用于獲取 StackOverflowError
這以 StackOverflowError 告終,因為 ClassOne 的構造函數正在實例化 ClassTwo,而 ClassTwo 的構造函數又在實例化 ClassOne。

回首憶惘然
TA貢獻1847條經驗 獲得超11個贊
因為和類之間存在circular
依賴關系。您已經在 class和in class中創建了。所以當你嘗試創建它的對象時,它會嘗試創建它的對象,然后嘗試創建它的對象,它仍然會繼續并導致。Item
SlayerProgram
Item item = new Item(name,price,amount);
SlayerProgram
SlayerProgram sp = new SlayerProgram();
Item
Item
SlayerProgram
SlayerProgram
Item
method stack is not full
StackOverflow
添加回答
舉報
0/150
提交
取消