Search code examples
javaxpagesxpages-ssjsssjs

Xpages - Error calling method 'next()' on java class 'java.util.AbstractList$SimpleListIterator'


I'm trying to loop through attachments in a document using the iterator method before sending out the email including attachments. When trying to extract the attachments during the loop, getting the name of the files seems fine but getting the attachment path throws the above error.

This is written in ssjs, but calling a java class to send the email via SMTP.

 var bodyAttach = new java.util.Vector();
    var alist = nDoc.getAttachmentList("gDocs");
    if (!alist.isEmpty()) {
        var alisti = alist.iterator();
        //  Each element is of type NotesEmbeddedObject
        while (alisti.hasNext()) {
            attachname = alisti.next().getName();

            attachpath = java.lang.System.getProperty('java.io.tmpdir') + java.io.File.separator + attachname.replace(/ /g, '_');
            alisti.next().extractFile(attachpath);

            extattachname = @LowerCase(@RightBack(attachname, "."));

            if (extattachname == "gif" || extattachname == "jpg" || extattachname == "jpeg" || extattachname == "png") {
                res_attachpath = resizeImage(attachname,attachpath,1024,1024);
                if (res_attachpath != "") {
                    bodyAttach.add(res_attachpath);
                } else {
                    bodyAttach.add(attachpath);
                }
            } else {
                bodyAttach.add(attachpath);
            }
        }
    }
    vMail.setBodyAttach(bodyAttach);

    vMail.getBodyAttach();

It was expected to loop through the attachment, resize images before setting attachments on the email variable, but I get the error below:

Error calling method 'next()' on java class 'java.util.AbstractList$SimpleListIterator'

This points to line 10 of this code:

alisti.next().extractFile(attachpath); // error points to this line

Solution

  • You're calling .next() twice in your body. .next() gets the next element from the current position of the List and repositions. Only call .next() once within the loop. Set that to a variable. Or you're getting the next, and then the next, and then the next, without a check in between about whether or not you've reached the end of the List. Which is what's causing the error.