我遇到了一个错误,提示我java.lang.StackOverflowError: null
。我的程序有点像彩票,选择一个随机数字,并根据该数字从数组中选择您的项目。现在这一切都很方便而且花花公子,但是当我尝试将收到的元素插入库存时。我得到了那个错误。我很确定我的类设置错误,但我不知道如何在单独的类中构造一个数组,该数组从另一个类接收数据,并将数据返回到同一个类。闲聊已经够多了,这就是我的代码到目前为止的样子。 (请不要介意额外的变量,因为这是我的代码的裁剪部分)
public class inventory {
private int inventorymain;
public String[] inventorymain() {
String[] inventorymain;
return inventorymain();
}
}
import java.util.Scanner;
import java.util.Random;
public class glue {
public static void main(String [] args) {
inventory inv = new inventory();
allskins a = new allskins();
Scanner s = new Scanner(System.in);
int selection = 0;
int invcount = -1;
Random rand = new Random();
do {
System.out.println(d.menue());
selection = s.nextInt();
if (selection == 1) {
invcount++;
int random = rand.nextInt(208);
System.out.println("You opend a: ");
System.out.println(a.allskins()[random]);
System.out.println("Your item has been put in your inventory, select inventroy from the menue to view all items.");
System.out.println("");
inv.inventorymain()[invcount] = (a.allskins()[random]);
}
}while (selection != 6);
}
}
谢谢
最佳答案
您正在进入无限递归循环调用inventorymain()
public class inventory {
private int inventorymain;
public String[] inventorymain() {
String[] inventorymain;
return inventorymain(); // recursive call here without escape condition
}
}
最好保持方法名称和变量名称不同,以避免这种情况。
关于java - 多类项目中的空数组在调用时接收数据后返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44372311/