我的第一个代码块是我的Item Object文件; 第二个是主类.在运行代码没有任何问题之前,但在我添加了读写文件后,我的代码开始收到堆栈流错误.只是调用错误的片段.
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
}
Run Code Online (Sandbox Code Playgroud)
我的主要课程:
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.StackOverflowError
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)
Run Code Online (Sandbox Code Playgroud)
如何找到堆栈溢出的位置?
Item创造SlayerProgram:
SlayerProgram sp = new SlayerProgram();
Run Code Online (Sandbox Code Playgroud)
和SlayerProgram创建Item
Item item = new Item(name,price,amount);
Run Code Online (Sandbox Code Playgroud)
因此,在初始化时,您将无休止地创建这些对象
获取StackOverflowError 有一个类似的Baeldung示例
这最终导致StackOverflowError,因为ClassOne的构造函数实例化ClassTwo,ClassTwo的构造函数再次实例化ClassOne.
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |