title: Reading an XML file in Java having multiple attributes
body: <p>This is the XML file I'm working on:</p>
  
  <pre><code>&lt;book&gt;
      &lt;chapter index="1" name="Chapter 2"&gt;
          &lt;verse index="1" text="First Line" /&gt;
          &lt;verse index="2" text="Second Line" /&gt;
          &lt;verse index="3" text="Third Line" /&gt;
      &lt;/chapter&gt;
      &lt;chapter index="1" name="Chapter 2"&gt;
          &lt;verse index="1" text="First Line" /&gt;
          &lt;verse index="2" text="Second Line" /&gt;
          &lt;verse index="3" text="Third Line" /&gt;
      &lt;/chapter&gt;
  &lt;/book&gt;
  </code></pre>
  
  <p>This is what I desperately tried, following an answer posted: <a href="https://stackoverflow.com/questions/11560173/how-can-i-read-xml-attributes-using-java">How can I read Xml attributes using Java?</a></p>
  
  <pre><code>import java.io.File;
  
  import javax.xml.parsers.DocumentBuilder;
  import javax.xml.parsers.DocumentBuilderFactory;
  
  import org.w3c.dom.Document;
  
  
  public class Main
  {
      public static void main (String[] args)
      {
          try
          {
              File fXmlFile = new File("book.xml");
               DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
               DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
               Document doc = dBuilder.parse(fXmlFile);
               doc.getDocumentElement().normalize();
  
               System.out.println("Verse: " +
                       doc.getDocumentElement().getElementsByTagName("chapter").item(0).getChildNodes().item(0).getAttributes().getNamedItem("text").getNodeValue());
  
          }
          catch (Exception ex)
          {
              ex.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <h2><strong>I tried to print the first verse of each chapter, but got a Null Pointer exception</strong></h2>