为什么我收到堆栈溢出?
Why am I receiving a stack overflow?
我的第一个代码块是我的项目对象文件;第二个是主要 Class。在代码 运行 没有任何问题之前,但是在我添加了读写文件之后,我的代码开始收到堆栈流错误。只是调用错误的片段。
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
}
我的主class:
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.WhosebugError
at 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)
如何找到导致堆栈溢出的位置?
Item
创建 SlayerProgram
:
SlayerProgram sp = new SlayerProgram();
并且 SlayerProgram
创建 Item
Item item = new Item(name,price,amount);
因此在初始化时您将无休止地创建这些对象
有一个类似的 Baeldung example 用于获取 WhosebugError
This ends up with a WhosebugError since the constructor of ClassOne is instantiating ClassTwo, and the constructor of ClassTwo again is instantiating ClassOne.
因为 Item
和 SlayerProgram
class 之间存在 circular
依赖关系。您在 class SlayerProgram
中创建了 Item item = new Item(name,price,amount);
,在 class Item
中创建了 SlayerProgram sp = new SlayerProgram();
。因此,当您尝试创建 Item
的对象时,它会尝试创建 SlayerProgram
的对象,然后 SlayerProgram
尝试创建 Item
的对象,它仍在继续method stack is not full
并导致 Whosebug
.
我的第一个代码块是我的项目对象文件;第二个是主要 Class。在代码 运行 没有任何问题之前,但是在我添加了读写文件之后,我的代码开始收到堆栈流错误。只是调用错误的片段。
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
}
我的主class:
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.WhosebugError
at 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)
如何找到导致堆栈溢出的位置?
Item
创建 SlayerProgram
:
SlayerProgram sp = new SlayerProgram();
并且 SlayerProgram
创建 Item
Item item = new Item(name,price,amount);
因此在初始化时您将无休止地创建这些对象
有一个类似的 Baeldung example 用于获取 WhosebugError
This ends up with a WhosebugError since the constructor of ClassOne is instantiating ClassTwo, and the constructor of ClassTwo again is instantiating ClassOne.
因为 Item
和 SlayerProgram
class 之间存在 circular
依赖关系。您在 class SlayerProgram
中创建了 Item item = new Item(name,price,amount);
,在 class Item
中创建了 SlayerProgram sp = new SlayerProgram();
。因此,当您尝试创建 Item
的对象时,它会尝试创建 SlayerProgram
的对象,然后 SlayerProgram
尝试创建 Item
的对象,它仍在继续method stack is not full
并导致 Whosebug
.