0

This is my xml to parse

<metadata>
    <groupId>org.chromium</groupId>
    <artifactId>chromedriver-win32</artifactId>
    <versioning>
        <release>2.14</release>
        <versions>
            <version>2.14</version>
        </versions>
        <lastUpdated>20150610112540</lastUpdated>
    </versioning>
</metadata>

While I am trying to parse the above XML, I am getting NullPointerException even though I have handled the null check. Below is the code

private String getVersionFromNode(Element eElement) {
    String version = null;

    // Get the latest version
    String tagName = "latest";
    try {
        version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
        // If latest version is not available take the release
        // version
        if ( version.isEmpty() ||  null == version || "null" == version) {
            tagName = "release";
            version = eElement.getElementsByTagName(tagName).item(0).getTextContent();
        }
    }
    catch (NullPointerException e) {
       System.out.println("not able to parse " + tagName + " tag " + e.getMessage());
    }

    return version;
}

When the version is null, it should enter in below block.

if ( version.isEmpty() ||  null == version || "null" == version) {
    tagName = "release";
    version = eElement.getElementsByTagName(tagName).item(0).getTextContent();

}

What should be solution for this problem?

Aakash
  • 2,029
  • 14
  • 22
Psl
  • 3,830
  • 16
  • 46
  • 84

3 Answers3

3

Your String tagName is "latest" and in your xml file, there is no element named latest and so your version remains null and therefore a NullPointerException is generated when the code:

version.isEmpty()  

is executed in your if statement. Because version is null. And your code tries to check if null is empty. Which it cannot. And so, a NullPointerException is generated here.

To handle this situation, first check if version is null. Secondly check if the string version is equal to string "null". And thirdly check if it is empty. So change the sequence of conditions in your if statement from:

if ( version.isEmpty() ||  null == version || "null" == version)  

to

if (null == version || "null" == version || version.isEmpty())  

By the way, as a side tip, use:

"null".equals(version)  

rather than

"null" == version
Aditya Singh
  • 2,343
  • 1
  • 23
  • 42
2

Change your condition

if ( version.isEmpty() ||  null == version || "null" == version)

to

if (null == version || "null".equals(version) || version.isEmpty())
Aakash
  • 2,029
  • 14
  • 22
1

When chaining together method calls, you risk a NullPointerException at every step. For example, in your line

version = eElement.getElementsByTagName(tagName).item(0).getTextContent();

there are 4 opportunities for a NullPointerException:

  • if eElement is null, trying to call getElementsByTagName(tagName)
  • if calling getElementsByTagName(tagName) when tagName is null
  • if getElementsByTagName(tagName) returns null, trying to call item(0)
  • if item(0) returns null, trying to call getTextContent()

Not all of these will be possible in your code because tagName is specified, but you should always beware.

An alternative approach for your problem would be to use XPath

See also How to read XML using XPath in Java

Community
  • 1
  • 1
NickJ
  • 9,380
  • 9
  • 51
  • 74