0
該程序在屏幕上隨機創建10個方塊,然後10個點,然後使用for循環來比較每個方塊與每個方塊來決定是否該點在正方形內,但每當我運行此代碼,此行(處理)當我嘗試運行此代碼時,我得到一個空指針異常
points[i].x > squares1[j].x && points[i].x < squares2[j].x && points[i].y > squares1[j].y && points[i].y < squares2[j].y
返回一個空指針異常,我不能爲我的生命找出原因,任何人都可以請幫助。
主代碼
PVector[] points;
PVector[] squares1;
PVector[] squares2;
void setup()
{
size(800, 800);
points = new PVector[10];
squares1 = new PVector[10];
squares2 = new PVector[10];
for(int i = 0; i < 10; i+= 80)
{
squares1[i] = new PVector(random(0, width-80), i);
squares2[i] = new PVector(squares1[i].x+80, squares1[i].y+80);
rect(squares1[i].x, squares1[i].y, 80, 80);
}
for(int i = 0; i < points.length; i++)
{
points[i] = new PVector(random(width), random(height));
for(int j = 0; j < 10; j++)
{
if(points[i].x > squares1[j].x && points[i].x < squares2[j].x && points[i].y > squares1[j].y && points[i].y < squares2[j].y)
{
fill(255, 0, 0);
ellipse(points[i].x, points[i].y, 5, 5);
println("Point " + i+1 + " is contained in figure " + j + ".");
}
else
{
fill(0);
ellipse(points[i].x, points[i].y, 5, 5);
println("Point " + i+1 + " is not contained in any figure.");
}
}
}
}
void draw()
{
}
謝謝你,你是一個救生員 – Allir
接受答案。 –
將來,要自己調試問題的方法是在循環之前,之內和之後打印出相關的變量。你會注意到'我'只會被打印一次,然後你可以檢查爲什麼會出現這種情況。 – kevinsa5