aspose file tools*
The moose likes XML and Related Technologies and the fly likes java.lang.NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "java.lang.NullPointerException " Watch "java.lang.NullPointerException " New topic
Author

java.lang.NullPointerException

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

Kindly let me know why its throwing java.lang.NullPointerException ?



Best regards
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4392
    
    8

It might help if you tell us where it's throwing it. The exception details will tell you exactly which line it happens on, which makes it much easier for us to help you.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Stack trace is showing problem with line 189 i.e.


thanks for replying
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12788
    
    5
I would also suspect code like:


because many Node types, such as Element, return null as a nodeValue - see the org.w3c.dom.Node JavaDocs very handy table.

The text associated with an Element is a Text type Node child of the element.

Bill
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Can you please tell me how can I fix it?

Best regards
g tsuji
Ranch Hand

Joined: Jan 18, 2011
Posts: 516
    
    3
>Can you please tell me how can I fix it?
Replace getNodeValue() by getTextContent(), if you're working on jdk 1.5+.

To my opinion, the most important error which does not give rise to any exception is your use of "index" where it is not meant to use. Replace index by 0, starting from emailElement onward, even though you ought to control the existence of those elements.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

Thanks for your favorable reply. I change the code and its working fine now but when the xml tag has no data then its throwing NullPointerException. I tried to correct but unable.


I tried to cover like this:


but not working.

Best regards
>
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
The first step is to find out which object is null. Unless you know that, everything else is speculation.


Ping & DNS - my free Android networking tools app
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
The xml file will be dynamically generated but I know about all the xml tags. This is difficult to say that which tags might be null but I have to explicitly mention for all the attributes.

Presently FirstName attribute is null and am trying to correct but still its throwing NPE. Any suggestion please

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
That doesn't help us. Which object in which line of code is null? Which XML are you feeding it? Have you tried creating a minimal test case (both in terms of XML and Java code) that exhibits the problem?
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
hi,

Thanks all for the help. I fixed it like this:

Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41874
    
  63
The code looks rather brittle, i.e. it may break with little changes in the XML because you're checking hardly any objects for being null before using them. Proceed with caution.
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Dear Ulf Dittmer,

1) Proceed with caution?? can you please be more specific?
2) How can I clean the code. I mean to remove extra code from it?
3) The output is here there like first name is coming at end and last name coming at the middle etc. the while loop is not printing the output in a sequence


Why this code is not printing the values randomly instead of ascending order? How can I make an order?


This question marked solved. I think I should start new thread

Thanks & best regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.lang.NullPointerException