This sometimes happens when you try to add a node to a document, but the node actually belongs to some other document (or doesn't belong to any document at all). If that's your problem, then check out the API documentation for Document, and you'll see there's a method which allows you to import (I think that's the
word) a node into a document. Once you've done that, you can then add it wherever you like in that document.