0

I have the following code:

public static void main(String[] args) 
    throws ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    dbFactory.setNamespaceAware(true);
    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    Document document = dBuilder.parse("NewFile.xml");
    String usr = document.getElementsByTagName("username").item(0).getTextContent();
    System.out.println(document);

}

This is causing a NullPointerException at String usr line. Any ideas why? The new file is placed in the workspace project folder and i also tried to give the absolute path with no success.

XML file:

    <?xml version="1.0"?>
<tudorTestUser>
    <propertySet>
        <property name="username">TudorTest123</property>   
        <property name="firstName">Ion</property>
        <property name="lastName">Vasile</property>
        <property name="password">Tester123</property>
        <property name="passwordAgain">Tester123</property>
    <!-- <property name="location">Bucharest</property> -->
        <property name="email">tester@test.com</property>
        <property name="Status">active</property>
        <property name="thirdParty">No Third Party</property>
        <property name="agentID">Tester</property>
        <property name="switch">on</property>
        <property name="position">Web Master</property>
        <property name="clone"></property>
        <property name="overrideCode"></property>   
    </propertySet>
</tudorTe
Tudor
  • 199
  • 3
  • 14

0 Answers0