我不能这样做吗?收到“例外”错误 [英] Can I not do this? Getting an 'exception' error
本文介绍了我不能这样做吗?收到“例外”错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
得到以下错误: .Exception in thread main java.lang.NullPointerException
Getting the following error: ".Exception in thread "main" java.lang.NullPointerException "
// Creates 10 accounts
Account[] AccArray = new Account[10];
// Data fields
double atmVal;
// Sets each account's discrete id and initial balance to 100
for (int i=0;i<10;i++){
AccArray[i].setId(i); // this line is specified in the error
AccArray[i].setBalance(100);
}
这个编译的很好,但是我得到了例外(不确定那些是)。
This compiled fine, but I get an "exception" (not sure what those are, yet).
我根本看不出什么问题,至少在这里看不到。在这种情况下,我将添加更多代码。
I don't see what's wrong at all, at least not here. If that's deemed to be the case, I'll add more of my code.
推荐答案
您的数组已经初始化为包含10个帐户,但它们仍然为空。将循环更改为:
Your array is already intialized to contain 10 accounts, but they are all still null. Change your loop to be:
for (int i=0;i<10;i++){
ArrArray[i] = new Account(); // whatever constructor parameters are needed
AccArray[i].setId(i); // this line is specified in the error
AccArray[i].setBalance(100);
}
话虽如此,我建议您使用小写字母命名变量(例如 accArray
)。
That being said I recommend you name your variables with lowercase names (e.g. accArray
).
这篇关于我不能这样做吗?收到“例外”错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文