我希望能夠創造這樣的事情:試圖建立一個結構體數組的Java
int points = 250;
public class point {
float x, y;
}
point[] p = new point[points];
// constructor
public JavaMain() {
Random r = new Random();
for (int i = 0; i < points; i++) {
int tx = r.nextInt(640) + 1;
int ty = r.nextInt(480) + 1;
p[i].x = (float)tx;
p[i].y = (float)ty;
}
}
的問題是,這種試圖將值分配給p[i].x
或p[i].y
NullPointerException
這是我在Java上的第一天,已經通過谷歌和StackOverflow做了一些研究,但沒有發現任何引起問題的核心問題。
異常的等值線「主」顯示java.lang.NullPointerException – cookertron
你需要初始化數組中的元素,以及不只是數組引用 –
你能給我舉一個例子,請? – cookertron