cancel
Showing results for 
Search instead for 
Did you mean: 

XML parsing error in portal application...

Former Member
0 Kudos

Hello,

I have this exception java.lang.NullPointerException# with a xml like this

<users><user><email></email></user></users>

When email is filled like this it's worked but when it is empty no...

<users><user><email>test@test.com</email></user></users>

The code is like this :

NodeList EmailList = firstPersonElement.getElementsByTagName("email");

Element EmailElement = (Element)EmailList.item(0);

NodeList textEmailList = EmailElement.getChildNodes();

if (textEmailList == null)

{

System.err.println("null !");

}

String Email = null;

Email = ((Node)textEmailList.item(0)).getNodeValue().trim();

System.err.println("Email : " + Email);

How can I resolve it ?

Thanks a lot

best regards

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

HI,

There is a option available in NWDS to catch the exception at the point its getting throwed. Its available in debug mode.

Hope this will help you to find the exact place of the exception.

regards

Vivek Nidhi.

Answers (2)

Answers (2)

Former Member
0 Kudos

Hi Aurélien!

I think the problem is here:

Email = ((Node)textEmailList.item(0)).getNodeValue().trim();

Try:

myvar = ((Node)textEmailList.item(0)).getNodeValue();

if (Email!=null) Email=myvar.trim();

Hope this helps

Former Member
0 Kudos

Hello,

these two solutions are not working. I didn't find any tips at this time ... Helpful and rewards for help ! Thanks

Best regards,

Former Member
0 Kudos

> Hello,

> these two solutions are not working. I didn't find

> any tips at this time ... Helpful and rewards for

> help ! Thanks

> Best regards,

In that case the other possibility I could think of is that

textEmailList is not null, but either textEmailList.item(0)

or ((Node)textEmailList.item(0)).getNodeValue() is null.

You can work around this by enclosing the line


Email = ((Node)textEmailList.item(0)).getNodeValue().trim();

in an if-block:


if (textEmailList != null
        && textEmailList.item(0) != null
        && ((Node)textEmailList.item(0)).getNodeValue() != null) {
  Email = ((Node)textEmailList.item(0)).getNodeValue().trim();
}  

Best regards,

Jens

Former Member
0 Kudos

Hello,

my guess is that in the case of an empty <email> element, your textEmailList reference should already be null (you should also have the "null !" output in your error stream), so a NPE will be inevitable in the line


Email = ((Node)textEmailList.item(0)).getNodeValue().trim();

In order to prevent that, you could simply put the three lines below the if() {} construct in an else {} block:


NodeList EmailList = firstPersonElement.getElementsByTagName("email"); 
Element EmailElement = (Element)EmailList.item(0);
NodeList textEmailList = EmailElement.getChildNodes();

if (textEmailList == null) {
  System.err.println("null !"); 
}
else {
  String Email = null; 
  Email = ((Node)textEmailList.item(0)).getNodeValue().trim();
  System.err.println("Email : " + Email);
}

Does this help?

Best regards,

Jens