【发布时间】:2014-01-06 05:41:27
【问题描述】:
我收到以下错误:
array required, but java.lang.String found
我不知道为什么。
我要做的是将一个对象的实例(我相信这是正确的术语)放入该类型的类(对象)的数组中。
我有课:
public class Player{
public Player(int i){
//somecodehere
}
}
然后在我的 main 方法中创建它的一个实例:
static final Player[] a = new Player[5]; // this is where I'm trying to create the array.
public static void main(String[] args){
Player p = new Player(1);
a[0] = p; //this is the line that throws the error
}
有什么想法吗?
【问题讨论】:
-
您没有向我们展示引发异常的代码。这段代码很好。
-
向我们提供异常发生的代码!
-
保存并重新编译。
-
尝试在调试器中单步执行您的代码,以确定失败的确切行以及当时的相关变量。
-
这是编译时错误还是运行时异常?该消息看起来像是一个编译时问题。
标签: java arrays object instance