• Post Reply Bookmark Topic Watch Topic
  • New Topic

java.lang.NullPointerException when retriving second child XML Java  RSS feed

 
Ana-Maria Salai
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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

 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • 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?
 
Campbell Ritchie
Marshal
Posts: 56553
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Won't the code above give you childNode = null on the last sibling?
 
Tim Harris
Ranch Hand
Posts: 57
3
Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • 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: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
Chrome Eclipse IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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.
 
Liutauras Vilda
Sheriff
Posts: 4921
334
BSD
  • Likes 1
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!