0

I am getting sometimes problems with creating a list of names of the attached files in a NotesDocument. The custom message looks as followed:

AttachmentDominoDAO - General problem during reading attachment from entry 39E411CEC4AD22F3C1258821003399EF in database mail.nsf. fileObject.getName() returns null. Files found in document [contract.pdf]

Here is the method that I am calling:

private Attachment loadFromEntry(ViewEntry entry) {
        utils.printToConsole(this.getClass().getSimpleName().toString() + " - loadFromEntry(...) unid=" + entry.getUniversalID());
        Attachment attachment = new Attachment();
        try{
            
            attachment.setUnid(entry.getUniversalID()); 
            Document doc = entry.getDocument();
            if (null != doc){               
                attachment.setCreated(doc.getCreated().toJavaDate());               
                if(doc.hasItem("$FILE")){                   
                    List<String> files = doc.getAttachmentNames();
                    for (int j = 0; j < files.size(); ++j) {                         
                        EmbeddedObject fileObject = doc.getAttachment(files.get(j));                        
                        if(null != fileObject.getName()) {
                            if(null != fileObject.getName()) {
                                attachment.setFile(fileObject.getName());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.getName() returns " + fileObject.getName(), Level.SEVERE, null);
                            }

                            if(null != fileObject.getName() && !utils.Right(fileObject.getName(),".").isEmpty()) {
                                attachment.setExtension(utils.Right(fileObject.getName(),".")); 
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", extension is empty for file " + fileObject.getName(), Level.SEVERE, null);
                            }
                            
                            attachment.setSizeHuman(FileUtils.byteCountToDisplaySize(fileObject.getFileSize()));                            
                            
                            if(fileObject.getFileSize() > 0) {
                                attachment.setSize(fileObject.getFileSize());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", fileName.size() returns " + fileObject.getFileSize(), Level.SEVERE, null);
                            }

                            if(null != doc.getAuthors() && null != doc.getAuthors().firstElement()) {
                                attachment.setCreator(doc.getAuthors().firstElement());
                            } else {
                                XspOpenLogUtil.logEvent(null, "Problem with reading attachment from entry " + entry.getUniversalID() + ", doc.getAuthors().firstElement() returns " + doc.getAuthors().firstElement(), Level.SEVERE, null);
                            }

                            String fieldName = "type";
                            if (doc.hasItem(fieldName)) {
                                attachment.setType(fieldName);
                            }
                            
                        }else {
                            XspOpenLogUtil.logEvent(null, "AttachmentDominoDAO - General problem during reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName() + ". fileObject.getName() returns null. Files found in document " + doc.getAttachmentNames().toString(), Level.SEVERE, null);
                        }
                    }                   
                }           
            }
        }catch (Exception e) {
            XspOpenLogUtil.logEvent(e, "General problem with reading attachment from entry " + entry.getUniversalID() + " in database " + entry.getDocument().getParentDatabase().getFileName(), Level.SEVERE, null);
        }
        return attachment;
    }   

One document can only contain one file. When I check the document there is only one attachment and the attachment mentioned in the error message.

Anyone has a suggestion how to fix this issue?

Note: in 99% of the cases the error does not occur.

Patrick Kwinten
  • 1,988
  • 2
  • 14
  • 26
  • Just reading the code, it looks as though the fileObject itself is null. I'm not sure why it is. Can you try printing out the value for `files.get(j)` before the line that begins `EmbeddedObject fileObject`. It may be a blank string. – Rob Mason Apr 14 '22 at 08:55

2 Answers2

1

If getName() returns null for an attachment, try using getSource() instead. As long as it's an actual attachment (as opposed to an OLE embedded object), then getSource() always returns the original file name.

Scott Leis
  • 2,810
  • 6
  • 28
  • 42
0

NotesDocument.getAttachment does not find attachments that were created in rich text fields. It only finds attachments that were created directly in the document itself.

We used to call an attachment that is directly acceessed through the document a "V2 attachment" because that's the way things worked in Notes V2 -- over 30 years ago. Objects created through OLE launch properties on the form can also attach objects that are directly in the document instead of inside rich text. Since OLE and V2 are both relics of the deep, dark past, almost all attachments are created inside rich text fields these days.

Attachments that are inside rich text fields are accessed through the getEmbeddedObject method of the RichTextItem class.

Richard Schwartz
  • 14,463
  • 2
  • 23
  • 41
  • 1
    Your first paragraph is incorrect according to the Help, which says the following for `getAttachment`: "You can use this method to find file attachments that are not contained in a rich text item (such as an attachment in a Release 2 database) as well as file attachments that are contained in a rich text item." – Scott Leis Apr 19 '22 at 04:36
  • Yes, I misremembered this for sure. Too many old cobwebs. Should have dug up some old code to look at it. – Richard Schwartz Apr 20 '22 at 12:24
  • I gues the problem lies here: EmbeddedObject fileObject = doc.getAttachment(files.get(j)); fileObject is sometimes null. In the Help db i find that I could call on a document -> v = doc.getEmbeddedObjects(); and then loop through the vector. If that will provide a nullpointer to an embedded object I will eat my hat. – Patrick Kwinten Apr 22 '22 at 12:06
  • I seem to recall having to resort to using NotesSession.Evaluate to invoke @AttachmentNames in one implementation. That was a long time ago, and I can't remember what the case was, but it might have had something to do with those attachments that pop up every now again with fake names like "ATT001". – Richard Schwartz Apr 23 '22 at 13:30