0

I am trying to build server that sends a xml file to client. I am getting info from db and wants to build from that xml file.

But I have a problem with:

DocumentBuilder documentBuilder = null;
Document doc =documentBuilder.newDocument();

I am getting NullPointerException. Here is me full code:

public void createXmlTree() throws Exception {
  //This method creates an element node
  DocumentBuilder documentBuilder = null;
  Document doc =documentBuilder.newDocument();

  Element root = doc.createElement("items");
  //adding a node after the last child node of the specified node.

  doc.appendChild(root);
  for(int i=0;i<db.stories.size();i++){
    Element child = doc.createElement("item");
    root.appendChild(child);

    Element child1 = doc.createElement("title");
    child.appendChild(child1);

    Text text = doc.createTextNode(db.stories.get(i).title);
    child1.appendChild(text);

    //Comment comment = doc.createComment("Employee in roseindia");
    //child.appendChild(comment);

    Element child2 = doc.createElement("date");
    child.appendChild(child2);

    Text text2 = doc.createTextNode(db.stories.get(i).date);
    child2.appendChild(text2);

    Element child3 = doc.createElement("text");
    child.appendChild(child3);

    Text text3 = doc.createTextNode(db.stories.get(i).text);
    child3.appendChild(text3);

    root.appendChild(child3);
0

4 Answers 4

2

Well yes, you would get a NullPointerException. You're calling a method on a null reference - very clearly, given that you've assigned the documentBuilder a null value on the line before. You need to get an instance of DocumentBuilder to start with. For example:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
Sign up to request clarification or add additional context in comments.

2 Comments

if I will do that I am getting symbol: method createElement(java.lang.String) location: variable doc of type javax.xml.parsers.DocumentBuilde on doc.appendChild(root);
Well that suggests you've got a variable of type DocumentBuilder called doc - I would expect doc to be a Document variable, after calling documentBuilder.newDocument. You've still got to make that call - my code just showed you how to get a DocumentBuilder.
1

of course you are getting a NullPointerException, your DocumentBuilder is null. Try instantiating it first.

    // Step 1: create a DocumentBuilderFactory
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

    // Step 2: create a DocumentBuilder
    DocumentBuilder db = dbf.newDocumentBuilder();

1 Comment

if I will do that I am getting symbol: method createElement(java.lang.String) location: variable doc of type javax.xml.parsers.DocumentBuilde on doc.appendChild(root);
1

Guys are right about DocumentBuilder. But may I offer you other solution? Your servlet mostly deals with generating of XML itself, i.e. produces kind of markup. This is the purpose of JSP. You can implement simple JSP page that will actually contain template of your XML and some code that inserts dynamic data. This is much simpler and easier to maintain.

Yes, JSP typically generate HTML but no-one said that they cannot generate XML or any other text format. Just do not forget to set content type to text/xml.

Comments

1

Do you really need to write you XML manually? Do you have the XSD of the XML you want to write?

Because, it would be easier to generate some classes using XJC/JAXB and use the marshaller to write your XML file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.