0

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.

1
  • When aksing questions related to Exceptions you encounter. It's good practice to include the stacktrace of the Exception. This helps a lot! Commented Oct 11, 2018 at 9:16

3 Answers 3

1

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();
        **
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.