简体   繁体   English

java.lang.IllegalStateException:带有迭代器的null

[英]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. 由于某种原因,我从以前运行良好的方法中获取了java.lang.IllegalStateException:null异常。 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. 它只是突然停止工作,并且不会在每个条目上抛出错误,只是列表中的4.。 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. 是抛出错误iter.remove()部分的特殊行。 status is false, as it should. status应该是假的。

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. 现在,如果第一个条件为真(并且iter.remove() ),则将不执行else子句。

I also suggest to avoid the NullPointerException instead of catching it. 我还建议避免NullPointerException而不是捕获它。 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? 您的代码中有一个IF和一个IF-ElSE,是想要的还是错过了“ else”? Without this "else" you are likely to call iter.remove() more that once in an iteration. 如果没有这个“ else”,您可能会在一次迭代中多次调用iter.remove()。

        **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());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:已经获得迭代器 - java.lang.IllegalStateException: Iterator already obtained iterator.remove() 中的 java.lang.IllegalStateException - java.lang.IllegalStateException in iterator.remove() java.lang.IllegalStateException:目标主机为null - java.lang.IllegalStateException: Target host is null java.lang.IllegalStateException:[TextView]不能为null - java.lang.IllegalStateException: [TextView] must not be null java.lang.IllegalStateException - java.lang.IllegalStateException java.lang.IllegalStateException目标主机不能为null连接到数据库 - java.lang.IllegalStateException Target host must not be null connecting to database java.lang.IllegalStateException:Parent不为null,但此组件不相关 - java.lang.IllegalStateException: Parent was not null, but this component not related java.lang.IllegalStateException:无法为unitName null检索EntityManagerFactory - java.lang.IllegalStateException: Unable to retrieve EntityManagerFactory for unitName null java.lang.illegalstateexception无法检索单位名称为null的entitymanagerfactory - java.lang.illegalstateexception unable to retrieve entitymanagerfactory for unitname null 击中java.lang.IllegalStateException:在HttpSession中调用setAttribute()时为null - Hitting java.lang.IllegalStateException: null when call setAttribute() in HttpSession
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM