我认为这很简单,因为我很确定我以前做过,但我似乎无法让它发挥作用。我的课是:
public class City
{
String start = null;
String end = null;
int weight = 0;
}
我正在做:
City cityGraph[] = new City[l];
例如,当我尝试访问 cityGraph[x].start 时,我得到一个空指针异常,所以我想我还需要初始化数组中的每个元素,所以我这样做:
for(int j = 0; j < l; j++)
{
cityGraph[j] = new City();
}
但它给了我这个错误:
No enclosing instance of type Graphs is accessible.
Must qualify the allocation with an enclosing instance
of type Graphs (e.g. x.new A() where x is an instance of Graphs).
我不知道这意味着什么,或者如何解决它。任何帮助,将不胜感激!