Why am I receiving a stack overflow?(为什么我收到堆栈溢出?)
本文介绍了为什么我收到堆栈溢出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的第一个代码块是我的Item对象文件;第二个是Main 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
}
我的主类:
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)
如何找到导致堆栈溢出的位置?
推荐答案
Item
创建SlayerProgram
:
SlayerProgram sp = new SlayerProgram();
和SlayerProgram
创建Item
Item item = new Item(name,price,amount);
因此在初始化时,您将无休止地创建这些对象
获取StackOverflow Error有类似的Baeldung example
这以StackOverflow Error结束,因为ClassOne的构造函数实例化ClassTwo,而ClassTwo的构造函数再次实例化ClassOne。
这篇关于为什么我收到堆栈溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:为什么我收到堆栈溢出?


猜你喜欢
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 获取数字的最后一位 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 转换 ldap 日期 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01