title: Loop through all elements in XML using NodeList
body: <p>I want to loop through all elements in a piece of XML printing each one.  My problem is that I keep getting a null pointer exception after the <code>staff1</code> tag, i.e. <code>john 465456433 gmail1 area1 city1</code></p>
  
  <p>This my Java code to print all elements in an xml file:</p>
  
  <pre><code>File fXmlFile = new File("file.xml");
  DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
  DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
  Document doc = dBuilder.parse(fXmlFile);
  
  doc.getDocumentElement().normalize();
  
  System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
  
  NodeList nList = doc.getElementsByTagName("*");
  
  System.out.println("----------------------------");
  
  Node n=null;
  Element eElement=null;
  
  for (int i = 0; i &lt; nList.getLength(); i++) {           
    System.out.println(nList.getLength());     
    n= nList.item(i);                            
    System.out.println("\nCurrent Element :" + n.getNodeName());
  
  
    if (n.getNodeType() == Node.ELEMENT_NODE) {
      eElement = (Element) n.getChildNodes();
      System.out.println("\nCurrent Element :" + n.getNodeName());
      name = eElement.getElementsByTagName("name").item(i).getTextContent(); //here throws null pointer exception after printing staff1 tag
      phone = eElement.getElementsByTagName("phone").item(i).getTextContent();
      email = eElement.getElementsByTagName("email").item(i).getTextContent();
      area = eElement.getElementsByTagName("area").item(i).getTextContent();
      city = eElement.getElementsByTagName("city").item(i).getTextContent();
    }
    n.getNextSibling();
  }
  </code></pre>
  
  <p>XML File:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;company&gt;
    &lt;staff1&gt;
      &lt;name&gt;john&lt;/name&gt;
      &lt;phone&gt;465456433&lt;/phone&gt;
      &lt;email&gt;gmail1&lt;/email&gt;
      &lt;area&gt;area1&lt;/area&gt;
      &lt;city&gt;city1&lt;/city&gt;
    &lt;/staff1&gt;
    &lt;staff2&gt;
      &lt;name&gt;mary&lt;/name&gt;
      &lt;phone&gt;4655556433&lt;/phone&gt;
      &lt;email&gt;gmail2&lt;/email&gt;
      &lt;area&gt;area2&lt;/area&gt;
      &lt;city&gt;city2&lt;/city&gt;
    &lt;/staff2&gt;
    &lt;staff3&gt;
      &lt;name&gt;furvi&lt;/name&gt;
      &lt;phone&gt;4655433&lt;/phone&gt;
      &lt;email&gt;gmail3&lt;/email&gt;
      &lt;area&gt;area3&lt;/area&gt;
      &lt;city&gt;city3&lt;/city&gt;
    &lt;/staff3&gt;
  &lt;/company&gt;
  </code></pre>
  
  <p>Expected Output:</p>
  
  <pre><code>john
  465456433
  gmail1
  area1
  city1
  mary
  4655556433
  gmail2
  area2
  city2
  furvi
  4655433
  gmail3
  area3
  city3
  </code></pre>