title: Add elements to existing XML file using Java
body: <p>I have an existing blank XML file like this:</p>
  
  <pre><code>&lt;root&gt;
  &lt;!--Some Comment goes here&gt;
  &lt;/root&gt;
  </code></pre>
  
  <p>Now I want have a GUI which has 1 textfield and 1 textarea which takes some string(title and content) and upon clicking the button 'Save' it adds them to the XML file under the element. I have created root in a separate GUI and saved into 'sample.xml' and upon clicking a button, the content add window comes up, where I open the same XML file and want to add the items into that. But every time I do so, I am getting a 'Null Pointer Exception' error. The following is my code. Please tell me where I am going wrong:</p>
  
  <p>Code in the main GUI:</p>
  
  <pre><code>JButton btnNewButton = new JButton("Add New");
          btnNewButton.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
                  //File f = new File("sample.xml");
                  DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                  DocumentBuilder docBuilder;
                  try {
                      Writer output = new BufferedWriter(new FileWriter("sample.xml"));
                      docBuilder = dbfac.newDocumentBuilder();
                      Document doc = docBuilder.newDocument();
  
                      Element root = doc.createElement("root");
                      doc.appendChild(root);
  
                      Comment comment = doc.createComment("Just a thought");
                      root.appendChild(comment);
  
                      TransformerFactory transfac = TransformerFactory.newInstance();
                      Transformer trans = transfac.newTransformer();
                      trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                      trans.setOutputProperty(OutputKeys.INDENT, "yes");
  
                      StringWriter sw = new StringWriter();
                      StreamResult result = new StreamResult(sw);
                      DOMSource source = new DOMSource(doc);
                      trans.transform(source, result);
                      String xmlString = sw.toString();
  
                      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sample.xml", true)));
                      out.println(xmlString);
                      out.close();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (ParserConfigurationException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (TransformerException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  </code></pre>
  
  <p>Code in the secondary GUI to add elements:</p>
  
  <pre><code>JButton btnSave = new JButton("Save");
          btnSave.addActionListener(new ActionListener() {
              public void actionPerformed(ActionEvent arg0) {
  
                  DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                  dbfac.setIgnoringComments(true);
                  DocumentBuilder docBuilder;
                  try {
                      docBuilder = dbfac.newDocumentBuilder();
                      Document doc = docBuilder.parse("sample.xml");
  
                      NodeList rt = doc.getElementsByTagName("root");
  
                      Element child = doc.createElement("content");
                      child.setAttribute("title", textField.getText().toString());
                      rt.item(0).appendChild(child);
  
                      Text text = doc.createTextNode(textArea.getText().toString());
                      child.appendChild(text);
  
                      TransformerFactory transfac = TransformerFactory.newInstance();
                      Transformer trans = transfac.newTransformer();
                      trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                      trans.setOutputProperty(OutputKeys.INDENT, "yes");
  
                      StringWriter sw = new StringWriter();
                      StreamResult result = new StreamResult(sw);
                      DOMSource source = new DOMSource(doc);
                      trans.transform(source, result);
                      String xmlString = sw.toString();
  
                      PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("sample.xml", true)));
                      out.println(xmlString);
                      out.close();
  
  
                      System.out.println(xmlString);
                  } catch (ParserConfigurationException e) {
                      e.printStackTrace();
                  } catch (TransformerConfigurationException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (TransformerException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (FileNotFoundException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (IOException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  } catch (SAXException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
  
  
              }
          });
  </code></pre>