[英]“java.lang.NullPointerException” error
I'm getting an error "java.lang.NullPointerException" and BlueJ cannot seem to tell me what the error means. 我收到一个错误“ java.lang.NullPointerException”,BlueJ似乎无法告诉我该错误是什么意思。 Below is the code in which the error is appearing:
下面是出现错误的代码:
public int getJobsWaiting()
{
int count = 0;
int i = 0;
while (count < jobList.size())
{
Job temp = jobList.get(i);
if(jobList.get(count).isCompleted() != true)
{
count = count + 1;
}
i = i + 1;
}
return count;
}
either jobList
or jobList.get(count)
is null
. jobList
或jobList.get(count)
为null
。 And if you try to invoke a method on null
(in your case: size()
or isCompleted()
), you get exactly that, a NullPointerException
. 而且,如果尝试在
null
上调用方法(在您的情况下为: size()
或isCompleted()
),则可以得到一个确切的结果,即NullPointerException
。
try this, it is less code and only jobList can be null 试试看,这是更少的代码,只有jobList可以为null
int count = 0;
for (Job tmpJob : jobList) {
if (!tmpJob.isCompleted())
count++;
}
return count;
Your code is difficult to understand. 您的代码难以理解。 Variable temp is never used?
从未使用过可变温度吗?
With this, if a job is completed, count+1 is not executed and the while is never ended? 这样,如果作业完成,则不会执行count + 1,而while永不结束?
if(jobList.get(count).isCompleted() != true)
{
count = count + 1;
}
Check if this can help: 检查这是否有帮助:
public int getJobsWaiting(List<Job> jobList) {
int count = 0;
for (int i = 0; i < jobList.size(); i++) {
Job temp = jobList.get(i);
if (temp != null && !temp.isCompleted()) {
count++;
}
}
return count;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.