How could this code throw a null pointer exception?
for (Foo f : Vector<Foo> v)
{
f.doStuff(); // this line throws a NullPointerException
}
Even if the Vector is empty, shouldn’t the inside block just never be executed?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
Vectoris not empty. As you say, if it was then the loop body would not be executed.If you get an NPE on that line, it means that one (or more) of the elements of the
Vectorisnull.I should also point out that the example code is syntactically incorrect. It should probably read something like this: