简体   繁体   中英

java.lang.IllegalStateException: null with iterator

For some reason, I'm getting java.lang.IllegalStateException: null exception from the method that was working just fine before. I don't think I made any changes on this. It just suddenly stopped working and it doesn't throw an error on every entry, just the one that is 4. in the list. I can't even see anything different on that entry, it has all the properties it's supposed to have.

Iterator<Class> iter = contacts.iterator();

while (iter.hasNext()){
        Class holder = iter.next();
        try {
            if(dateNow.isBefore(holder.getStartDate())){
                iter.remove();
            }if(dateNow.isAfter(holder.getEndDate())){
                iter.remove();
            }else{
                boolean status = checkStatus(holder);
                if(!status){
                    iter.remove();
                }
            }
        }catch (NullPointerException e) {
            //No end-date or start date
            boolean status = checkStatus(holder);
            if(!status){
                iter.remove();
            }
            else if(dateNow.isBefore(holder.getStartDate())){
                iter.remove();
            }
        }
    }

is throwing this error. My only reason to use an iterator is that I can remove items while iterating it.

if(!status){
      iter.remove();
       }

is the spesific line throwing the error, iter.remove() part. status is false, as it should.

thanks for any help.

It looks like you might be trying to remove the same element twice from the iterator.

I suggest changing the logic to:

        if (dateNow.isBefore(holder.getStartDate())) {
            iter.remove();
        } else if (dateNow.isAfter(holder.getEndDate())) { // notice the change here
            iter.remove();
        } else {
            boolean status = checkStatus(holder);
            if(!status){
                iter.remove();
            }
        }

Now, if the first condition is true (and iter.remove() is called), the else clause won't be executed.

I also suggest to avoid the NullPointerException instead of catching it. For example:

    if (holder.getStartDate() != null && dateNow.isBefore(holder.getStartDate())){
        iter.remove();
    } else if(holder.getEndDate() != null && dateNow.isAfter(holder.getEndDate())){
        iter.remove();
    } else if (!checkStatus(holder)) {
        iter.remove();
    }

You have one IF and one IF-ElSE in your code, is that intended or you missed "else" there? Without this "else" you are likely to call iter.remove() more that once in an iteration.

        **if(dateNow.isAfter(holder.getEndDate())){
            iter.remove();
        **

I am not sure about your task, does this code resolve your problem?

contacts.stream().filter(x -> {
            if (dateNow.isBefore(holder.getStartDate()) || dateNow.isAfter(holder.getEndDate()) ){
                return false;
            }
            boolean status = chackStatus(x);
            if (!status){
                return false;
            }
            return true;
        }).collect(Collectors.toList());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM