Reputation: 21
I'm new to MarkLogic java API and trying to create an xml document where Document
is constructed using DocumentBuilderFactory
and DocumentBuilder
and everything is working fine with the following code.
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=factory.newDocumentBuilder();
Document doc=docBuilder.newDocument(); //Works fine
Now since I have doc reference I can call doc.CreateElement()
to create an xml structured document.
In the same way if I refer a Document using DOMHandle from com.marklogic.client.io.DOMHandle;
DOMHandle handle=new DOMHandle();
Document doc=handle.get();
doc.createElement(); //NULL POINTER EXCEPTION
Now the document reference created from handle gives an null pointer exception.
I understood that I am getting document from a getter method which returns an empty document but I am not trying to access anything from the empty document. Instead trying to create a document element using doc.createElement()
where null pointer exception is arising.
Please explain the issue.
Upvotes: 2
Views: 85
Reputation: 204
A DOMHandle
represents XML content as a DOM Document. It is not a factory which would create a DOM Document. The handle is just an adapter that wraps a document that we read from the database or create in Java. Unless explicitly set with the constructor DOMHandle(Document content)
or with the method public void set(Document content)
, the content of the DOMHandle would be null and hence the NullPointerException
. You should probably do one of these
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder=factory.newDocumentBuilder();
Document doc=docBuilder.newDocument();
// Build the Document completely and assign it to the handle and use the handle
DOMHandle handle = new DOMHandle();
handle.set(doc);
// or DOMHandle handle = new DOMHandle(doc);
// or DOMHandle handle = new DOMHandle().with(doc);
Upvotes: 3