Announcement Announcement Module
Collapse
No announcement yet.
Eternal loop? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Eternal loop?

    I have a question why this code causes an out of memory error:

    Set<ConstraintViolation<MyObject>> cv = validator.validate(instance, MyGroupToValidate.class);
    while (cv.iterator().hasNext()) {
    result.rejectValue(cv.iterator().next().getPropert yPath().toString(), null, cv.iterator().next().getMessage());
    }

    It feels to me that I've managed to create an eternal loop, but i just can't figure out where am i going wrong. This is hibernate validator in use.

  • #2
    Please use [ code][/code ] tags when posting code, that way it remains readable ...

    For starters your loop is flawed... You are calling next() twice!!!! Which basically means you are skipping items. Next to that y ou are recreating the items over and over... Due to the fresh iterator.

    I suggest a simple for loop.

    Code:
    Set<ConstraintViolation<MyObject>> cv = validator.validate(instance, MyGroupToValidate.class);
    for (ConstraintViolation<MyObject> violation : cv) {
    	result.rejectValue(violation.getPropertyPath(), null, violation.getMessage());
    }

    Comment

    Working...
    X