my question is really, very simple, but everything that I find on the Internet tells me that I am doing it right, but I obviously misunderstood something.
I have a simple, simple Java ListIterator that after a while has-hasNext () - loop returns null for next (). Here is the code with my comments on the debug state:
[...]
ListIterator<Role> rolesIterator = currentUser.getRoles().listIterator();
// rolesIterator is now: java.util.ArrayList$ListItr
while( rolesIterator.hasNext() ) {
Role roleObject = rolesIterator.next(); // extra step for debugging reasons
String role = roleObject.getName(); // NullPointerException - roleObject is null
[...]
In my thoughts, a loop should not be entered if there is no next () object - therefore I check the use of hasNext (). What did I misunderstand, and what is the right way?
source
share