Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

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



Best regards
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stack trace is showing problem with line 189 i.e.


thanks for replying
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please tell me how can I fix it?

Best regards
 
g tsuji
Ranch Hand
Posts: 666
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
>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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The first step is to find out which object is null. Unless you know that, everything else is speculation.
 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,

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

 
Ulf Dittmer
Rancher
Posts: 42968
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic