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]

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.
> 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). :)
> 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?
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?
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.