Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8445141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T09:35:05+00:00 2026-06-10T09:35:05+00:00

I am trying to add a tag and it’s value dynamically into the xml

  • 0

I am trying to add a tag and it’s value dynamically into the xml file through the following function.I am trying to add the tag named first-name and it value under the root tag. But while running the following snippet I am getting exceptions.

    public void write(String name) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();            
        Document document = db.newDocument();

        Element blobKey_E = document.createElement("first-name");
        blobKey_E.appendChild( document.createTextNode( name ) );
        // The following line produces an exception
        // LINE 27 
        document.getDocumentElement().appendChild(blobKey_E); // append the new tag under the root

        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        DOMSource source = new DOMSource(document);
        StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
        transformer.transform(source, result);            
    }catch(Exception exc) {
        exc.printStackTrace();
    }
}

exceptions :

java.lang.NullPointerException
at Beans.XmlBuilder.write(XmlBuilder.java:27)
at Servlets.tester.doGet(tester.java:26)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

I have highlighted line number 27 in the function above.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T09:35:07+00:00Added an answer on June 10, 2026 at 9:35 am

    document.getDocumentElement() in this case returns null

    The Javadoc for getDocumentElement() says the following about is functionality:

    This is a convenience attribute that allows direct access to the child node that is the root element of the document. 
    

    In your case, there is no root element attached to your DOM. You may want to do a document.appendChild(blobKey_E); to attach blobKey_E to the DOM as the root element.

    Ideally when you’re trying to build up an XML DOM, here’re the basic steps that you would need to follow:

    Create Document
    Create the root element and add it to the Document
    Create child elements and attach it to the root or another existing child node

    public void write(String name) {
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();            
            Document document = db.newDocument();
    
            Element blobKey_E = document.createElement("first-name");
            blobKey_E.appendChild( document.createTextNode( name ) );
    
            /*
             * Here blobKey_E is treated as the root element for the document that you've created
             */
            document.appendChild(blobKey_E); 
    //            // LINE 27 
    //            document.getDocumentElement().appendChild(blobKey_E); // append the new tag under the root
    
            /*
             * Post this point, if you do a document.getDocumentElement(), it will no longer return 
             * a nullpointerexception because blobKey_E will be treated as the root element.
             */
    
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            DOMSource source = new DOMSource(document);
            StreamResult result = new StreamResult(new File("/home/non-admin/NetBeansProjects/Personal Site_Testers/web/xml/xml_1.xml"));
            transformer.transform(source, result);            
        }catch(Exception exc) {
            exc.printStackTrace();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to add a description tag into the vssettings file so I can
Im trying to add a div tag dynamically with some data attributes but the
I'm trying to add an extra class tag if an element / value is
I'm trying to add some additional key/value pairs to an NSMutableDictionary, using: Tag *tag1
Im trying to add a tag to a group so when I want to
I'm trying to add the caption to the image title tag in wordpress so
We are trying to add a custom header (X-Robots-Tag) for sitemap files in IIS
I am trying to isolate and add a class to a clicked anchor tag.
I am trying add picture boxes dynamically. My code is as PictureBox picture =
I'm trying add a bold/strong tag the line headings of the follow string/text block:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.