所以我想提出一個簡單的小程序和IDE在這行拋出NullPointerException異常(http://puu.sh/5TJLZ.png)錯誤:total[0] = calc(coins[0],coins[1],coins[2]);
注意void setupCoins()
和String calc()
是獨立的標籤,而不是部分該標籤。NullPointerException異常的處理,找不到
int[] coins;
String[] total;
void setup(){
size(100,100);
setupCoins();
total[0] = calc(coins[0],coins[1],coins[2]);
saveStrings("data/balance.txt",total);
}
void setupCoins(){
String[] imports = loadStrings("balance.txt");
String[] numbers = split(imports[0],',');
coins = int(numbers);
}
String calc(int gold, int silver, int copper){
for(int i = 0; i <= copper; i++){
if(copper>9){
copper=copper-10;
silver++;
}
}
for(int i = 0; i <= silver; i++){
if(silver>9){
silver=silver-10;
gold++;
}
}
fill(#F5EE0A);
ellipse(20,20,10,10);
fill(#AFAFAF);
ellipse(20,45,10,10);
fill(#AA5C46);
ellipse(20,70,10,10);
fill(#000000);
text(gold + " Gold",30,25);
text(silver + " Silvers",30,50);
text(copper + " Coppers",30,75);
return gold + "," + silver + "," + copper;
}
那麼,哪個值爲'null'?如果我猜測,'total [0]'是無效的,因爲我看不到'total'的初始化位置。 – David
我沒有初始化它在第2行'String [] total;'? – CaptChilko
沒關係固定 – CaptChilko