In the case that status.getPlace()
is null and status.getUser().getLocation()
is not null and does not equal town
, you are going to get a NPE in the last part of the condition when status.getPlace().getName()
is called.
NullPointerException in Iterator
-
13-07-2023 - |
Domanda
I have a static method where I get a list of tweets and the name of a town as a parameter and I remove from the list whichever tweet did not originate from the town or the user that made it did not originate from the town.
Here is my code:
public static void removeIncorrectTowns(List<Status> tweets, final String town) {
if (town.isEmpty()) {
return;
}
Iterator<Status> it = tweets.iterator();
while (it.hasNext()) {
Status status = it.next();
if ((status.getPlace() == null && (status.getUser().getLocation() == null || !status.getUser().getLocation().equalsIgnoreCase(town)))
|| !status.getPlace().getName().equalsIgnoreCase(town)) {
it.remove();
}
}
}
The problem I have is that I got a NullPointerException
at line 62 which is the line with it.remove()
.
How is it even possible for the iterator to be null? That alone makes no sense to me since the while
loop checks if it.hasNext()
.
Soluzione
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow