NPE, but code still prints value

I'm getting a Null Pointer Exception when the following code is run:

privatedouble calculateParallelHBonding(LatticeStructure structure){

parallelHBonding = structure.getParallel().getNumHBonds()

* potential.getEnergyParallelHBond();

//parallelHBonding = hBonding.getNumParallelHBonds()

//* potential.getEnergyParallelHBond();

return parallelHBonding;

}

The commented out bit is just another way of doing the same thing. If I comment out the second and third lines, and uncomment the fourth and fifth, everything works fine. So that lead me to believe that the error is with the structure.getParallel().getNumHBonds [I know potential.getEnergyParallelHBond() works]. HOWEVER, if I put this:

if (structure.getParallel().getNumHBonds() == hBonding.getNumParallelHBonds()){

System.out.println("Match!");

}

in at the start of the method, it prints Match. Given that Java is telling me that the values from both ways of doing it are the same, why does one give an NPE? Surely a proper NPE error would break the code and prevent it from working out the value to use in the comparison?

Hope I've explained that sufficiently! Cheers.

Haydn

[1551 byte] By [haydnwa] at [2007-11-27 10:21:22]
# 1

I would have expected 'structure.getParallel()' to return null. Have you tried stepping through the code with a debugger?

EvilBroa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 2

In the constructor for the class relating to structure, I have:parallel = new HBondSheetParallel(this);

structure.getParallel() is:public HBondSheetParallel getParallel() {

return parallel;

}

Why would you expect it to return null (genuine question)? I'm using Eclipse to code and it doesn't highlight any problems, but haven't used a specific debugger.

haydnwa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 3

> Why would you expect it to return null (genuine question)?

I would have expected that because I think it seems a more likely source of the error than 'getNumHBonds()' to return null or you forgetting to pass a structure to the method. However, since you did the extra test, and that test did work, I'm still recommending the debugger (which I believe is a standard option of Eclipse). :)

EvilBroa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 4

I've just had a look at the debugger, with a break point at the end of the calculateParallelHBonding(LatticeStructure structure), running it with both ways of doing it. All the values look the same! The only null value for either way of doing it is for the object in which this method is situated. That' s because this method is called through the constructor for that object (so I assume the object doesn't technically exist yet).

haydnwa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 5

> I've just had a look at the debugger, with a break point at the end of the

> calculateParallelHBonding(LatticeStructure structure), running it with both

> ways of doing it. All the values look the same!

How do you even get to that breakpoint if one of the methods is 'supposed' to give a null-pointer exception?

EvilBroa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 6

This is precisely what's confusing me!! :( But this:private double calculateParallelHBonding(LatticeStructure structure) {

if (structure.getParallel().getNumHBonds() == hBonding.getNumParallelHBonds()) {

System.out.println("Match!");

}

parallelHBonding = (double) structure.getParallel().getNumHBonds()

* potential.getEnergyParallelHBond();

//parallelHBonding = hBonding.getNumParallelHBonds()

//* potential.getEnergyParallelHBond();

return parallelHBonding;

}

prints "Match!" and then bombs out with:java.lang.NullPointerException

at ....calculateParallelHBonding(Energy.java:93)

Line 93 is the "if" above, so if the NPE is occuring there, why does it still say that the method call gives a value equal to the hBonding.getnumParallelHBonds? Surely that would indicate that it's finished processing the "if", which must entail going past line 93 where the NPE supposedly occurs?

haydnwa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 7

Set a breakpoint at the 'if'. Step through the program. You'll be able to see what statement generates the exception.

EvilBroa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 8

I've done that now, and it steps through fine! No problem at all, it moves back to the method which calls calculateParallelHBonding(), and then onto the next method which is called. That's when stepping through a JUnit test; if I just run the JUnit test normally then it fails with an NPE.

I've obviously borked it good and proper. :( Looks like it's back to the drawing board.

haydnwa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...
# 9

All I can think of is that you call the method again somewhere. However if you didn't remove the breakpoint and just pressed continue, the debugger should also halt on that call. Other than that I have no idea what could be wrong... :(

EvilBroa at 2007-7-28 17:09:35 > top of Java-index,Java Essentials,New To Java...