I have List<Long> countriesList
which contains Long
values which are country ids.
Now I am iterating over some List<UserRequests>
list by using streams
.
userRequests.stream().
forEach(userRequest->
{
UserData=userRepository.findById(userRequest.getId()); //fine
if (CollectionUtils.isNotEmpty(countriesList) && countriesList.contains(userRequest.getCountryId()))//getting NPE here
{
//do some operation
}
});
I tried to debug by evaluating individual statements.
I have made sure countriesList
have some data
First part CollectionUtils.isNotEmpty(countriesList)
is returning true
.
userRequest
is also not null but userRequest.getCountryId()
is null.
And when I evaluated countriesList.contains(userRequest.getCountryId())
, I am getting Null pointer exception
here. why not false
?
I am confused what wrong am I doing.
Is default list.contains(null)
behaviour is like that only or is it because I am calling it within stream()
?
Just for simplification I have created simple list and compared with null
.
class Test {
public static void main(String[] args) {
List<Long> longList = List.of(1L, 2L, 3L);
System.out.println("Comparing long list with null::" + longList.contains(null));
}
}
This is the exception I am getting:
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at java.base/java.util.ImmutableCollections$AbstractImmutableList.indexOf(ImmutableCollections.java:166)
at java.base/java.util.ImmutableCollections$AbstractImmutableList.contains(ImmutableCollections.java:197)
at com.test.Test.main(Test.java:26)
But If I do:
List<Long> longList = new ArrayList<>();
longList.add(1L);
System.out.println("Comparing long list with null::" + longList.contains(null));
Why it is printing false
? Why no NullPointerException
here?