on 11-08-2007 2:54 AM
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
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
> 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
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
89 | |
10 | |
9 | |
9 | |
9 | |
6 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.