• 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 when retriving second child XML Java

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello. I am struggling with an error for some time but cannot get over it:
My code is the following:



I get java.lang.NullPointerException for childElementSecond = (Element) childElementFirst.getNextSibling();
Without childElementSecond the code works fine and it retrieves correctly the first element. Getting the second element is not necessarily; i can go even with the last one.
I have tried creating a new
Node childNode2 = eElement2.getLastChild();
Element childElementLast= (Element) childNode2 ; ...=> writer.println("next position: "+ childElementLast.getAttribute("ref"));
but it gives me null.
Thanks

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch, Ana-Maria!

When you post code, be sure to always UseCodeTags (← that's a link). I'll do it for you this time.

[Edit] I also broke up some long lines too.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you write this:

You aren't guaranteed to have a non-null childNode, are you? What about if the childNode is a leaf?
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think a little more explanation might be welcome.
Find yourself a diagram of a binary tree, or draw your own. If you have left and right branches, often shown by ↙ and ↘ symbols, eventually one of them will have nothing to point at. In the usual implementation of a binary tree, those will point to null. Look at this page from Microsoft and scroll about 35% of the way down. You will see a diagram only the arrows for left and right are a different shape, e.g. ↴ Note the … symbols in that diagram. The null means there is nothing to fill in the space shown by ….
 
Ana-Maria Salai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My xml has this format:

I need to find the ways those nodes belong to and retrieve another point from the way.
if (node id == ref) then i have a match (that node belongs to that way) so give me the id of the way and one more additional node (i don't care which one because i know it's in the same way)
but in this case i also need to do some checkups: if ( (node id == ref) and it is the first in the list) then for sure i need the next node in the list (or the last one). That's what i tried to do with childElementSecond = (Element) childElementFirst.getNextSibling();
I even tried to do it simply at the beginning and create Node childNode2 = eElement2.getLastChild(); but it returns null
childElementFirst works perfectly and in the above example it returns correctly 676633882
My ways contain a lot of points (min 5) so for sure i shouldn't get null.
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Won't the code above give you childNode = null on the last sibling?
 
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:
Won't the code above give you childNode = null on the last sibling?



I don't think so? childNode is not being assigned to anything in the while loop, so you'd be okay - you're just making the same call twice.
I think you may be able to condense it like this though?
 
Knute Snortum
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Harris wrote:
I don't think so?



Oops, no, you're right. But this line might get a NPE:
 
Tim Harris
Ranch Hand
Posts: 57
3
Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Knute Snortum wrote:

Tim Harris wrote:
I don't think so?



Oops, no, you're right. But this line might get a NPE:



Yep. Null pointer exceptions occur when you're trying to get data that simply isn't there. So chances are at some point there's a node or attribute that's showing up null.

If you're using an IDE, you can debug it and take a look at the different calls to see where you might be getting a null pointer exception - set up a few breakpoints where you're calling getNextSibling or getAttribute or getX, and see what those calls are returning.

If you're not using an IDE you can drop comments in using System.out.println to see where the call is failing.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Tim Harris wrote:If you're using an IDE, you can debug it and take a look at the different calls to see where you might be getting a null pointer exception - set up a few breakpoints where you're calling getNextSibling or getAttribute or getX, and see what those calls are returning.

If you're not using an IDE you can drop comments in using System.out.println to see where the call is failing.


Ana-Maria Salai, just for a general information:

If you're not using IDE, you still can use debugger - the one which comes along with JDK. It calls "jdb" - Java debugger.
It could be difficult and crazy to get start with it, as it would require to research a bit how to use it and understand it, but at the same time it could be funny to discover some tools included to JDK.

Quick basics:
1. When you compile your source code, you'd need to use -g option, so all debugging information would gets generated (class file would be slightly bigger than usual without -g option)
"javac -g ToDebug.java"

2. In order to start debugging, you could execute Java debugger by writing in terminal (unix like os) or commander (windows):
"jdb ToDebug"

3. Then you could set up breakpoint in the same way as you'd set up in IDE:
"stop at ToDebug:line-number" for example "stop at ToDebug:6"

After successfully set up break point, you should see similar message to:
"Deferring breakpoint Test:6.
It will be set after the class is loaded."

4. Then next step to execute debugging process:
"run"

5. At this point you should type "help" and search for an appropriate command: either you want to print local variables, or step into the function, or step over.. et cetera.

For more information from the very beginning you could find by typing "man jdb" (unix like os) in windows don't know how to access help. Maybe "jdb help" in commander.

It could be a funny challenging weekend's attraction though
 
Ana-Maria Salai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot for the feedback; i tried debugging but even so i couldn't manage it.
So i decided putting all the childNodes in an Array List and retrieve from there the elements that i want.
Now my problem is solved.
 
reply
    Bookmark Topic Watch Topic
  • New Topic