• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

java.lang.NullPointerException

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

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



Best regards
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Stack trace is showing problem with line 189 i.e.


thanks for replying
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you please tell me how can I fix it?

Best regards
 
Ranch Hand
Posts: 734
7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
>
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,

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

 
Ulf Dittmer
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic