Aghrr.. how can this give a NullPointerException ?

if (lineTotal ==null)

lineTotal =new Money(tot);

elseif (Math.abs(lineTotal.longValue() - tot) > 0){// < null pointer

overrides |= 1;

wildTotal = Math.abs(lineTotal.longValue() - tot) > 2;

}

No doubt I'll kick myself. Note - no mutli-threading going on here and this is 1.4 so no autoboxing.

[604 byte] By [malcolmmca] at [2007-11-27 7:41:08]
# 1
> No doubt I'll kick myself. Note - no mutli-threading> going on here and this is 1.4 so no autoboxing.What's the line (or two) before the null check?~
yawmarka at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 2

Nothing very interesting

long tot = (vatAmount != null) ? (net + vatAmount.longValue())

: (long) Math.round((float) net * (1f +

vatRate.getMultiplier()));

malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 3
[code snipped because it didn't reformat nicely]So Money is your class, and it has a longValue() method?How does it store the actual value? I'd guess as a BigDecimal ... are you sure that you're properly initializing whatever object it contains?
kdgregorya at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 4

Nothing jumps out at me immediately. I'd recommend refactoring a bit so you can isolate what's going on. Liberally sprinkle in some logging and/or use a debugger.

That, or post an SSCCE* that duplicates the problem.

[EDIT] No more links allowed, apparently. Not necessarily a bad thing at this point.

* http://mindprod.com/jgloss/sscce.html

~

yawmarka at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 5
No, it's stored as a long (in pence). And, were it that, the null pointer exception would surely hale from the constructor, not this bit of code. I'm suspecting that JDeveloper is screwing up the line association somehow.
malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 6
> How does it store the actual value? I'd guess as a> BigDecimal ... are you sure that you're> properly initializing whatever object it contains?Ah, that's probably it. Look at the longValue() method...~
yawmarka at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 7
Same objection - the top stack frame of the exception is right here, not in the Money class (which has been arround, unchanged, for a couple of years now, by the way).
malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 8
> Money class (which has been> arround, unchanged, for a couple of years now, by the> way).Your money must be different from my money... :pAnyway, stupid question: can you use a debugger? And did you do a re-build, just for the sake of it?
CeciNEstPasUnProgrammeura at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 9

> Same objection - the top stack frame of the exception

> is right here, not in the Money class (which has been

> arround, unchanged, for a couple of years now, by the

> way).

Can you post an SSCCE that duplicates the problem?

> Anyway, stupid question: can you use a debugger?

I don't think that's a stupid question. At least I hope not, since I mentioned it previously. :o)

~

yawmarka at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 10

> > Money class (which has been

> > arround, unchanged, for a couple of years now, by

> the

> > way).

>

> Your money must be different from my money... :p

>

> Anyway, stupid question: can you use a debugger?

Yes, but it doesn't seem to happen under the debugger.

>And

> did you do a re-build, just for the sake of it?

I did a make, but you're right, a rebuild might be worth a try.

malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 11
> Yes, but it doesn't seem to happen under the> debugger.Then I'm sure that the code you're executing is not the same as the stuff you're looking at.
CeciNEstPasUnProgrammeura at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 12

That was my first thought, too. But I've made changes in this class today, which have altered behaviour (i.e. it can't be an obsolete .class file). And even JDeveloper should be reliable enough to report the right line number in the source editor.

To confirm, I've just reformatted my source code, so that the offending line has moved to a new line number; still the exception stack frame indicates the same line (different line number).

Message was edited by:

malcolmmc

malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 13
Have you tried creating an SSCCE that duplicates the problem?~
yawmarka at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 14

Tricky, since I'd have to know what's causing the problem. In fact, I'm running the code under the debugger at the moment, with a breakpoint on the line giving me the error. Under the debugger the line isn't even executed (and, from what the program is doing I wouldn't expect it to be).

I'm starting to wonder if the JIT compiler is producing a wrong line number for the exception.

malcolmmca at 2007-7-12 19:21:43 > top of Java-index,Java Essentials,Java Programming...
# 15

> To confirm, I've just reformatted my source code, so

> that the offending line has moved to a new line

> number; still the exception stack frame indicates the

> same line (different line number).

One approach would be to use javap to dump the bytecode -- it could be that there's a typo that's unnoticeable looking at the Java code, but would appear in bytecode (ie, you see that the first "if" statement creates the new object and does astore_1, while the second "if" does an aload_2).

kdgregorya at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 16
> Tricky, since I'd have to know what's causing the problem.Yes, but I think a diligent refactoring and isolating the "moving parts" -- so to speak -- may shed light on exactly that.~
yawmarka at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 17

have you tried blocking your code into a couple of chucks and surrounding each chunck with a null pointer exception, each reporting a different error message until you get down to the offending line ? I know I have used that technique in the past when I didn't have a clue how something was getting a null pointer exception.

Aknibbsa at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 18
It's a long shot, but lineTotal isn't static by any chance is it? I remember DrLaszlo posting something about static variables not being recompiled unless you clean and rebuild? Or something? Maybe it was static final variables. Does anyone know what I'm talking about?
hunter9000a at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 19
http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.htmlThis, maybe (compile-time constants, about 2/3 down the page)?
kevjavaa at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 20

> It's a long shot, but lineTotal isn't static by any

> chance is it? I remember DrLaszlo posting something

> about static variables not being recompiled unless

> you clean and rebuild? Or something? Maybe it was

> static final variables. Does anyone know what I'm

> talking about?

I suppose I should ;-)

It was this:

public class C {

public static final String FOO = "bar";

}

public class Main {

public static void main(String[] args) {

System.out.println(C.FOO);

}

}

In this code, the value of C.FOO is folded into Main.class. So that if you edit C.java and make FOO = "hoppity", and rerun Main without recompiling it,

you will still see "bar" because of the folded-in constant.

I have no idea if that is relevant to this bug, but the way around this (apart from being smarter about compiling) is to write:

public class C {

public static final String FOO;

static {

FOO = "bar";

}

}

Hippolytea at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 21

Yeah, that's it!

Note: If a primitive type or a string is defined as a constant and the value is known at compile time, the compiler replaces the constant name everywhere in the code with its value. This is called a compile-time constant. If the value of the constant in the outside world changes (for example, if it is legislated that pi actually should be 3.975), you will need to recompile any classes that use this constant to get the current value.

Looks like it only applies to primitives and Strings through :(

Why did you change your username anyway Dr. Jamz?

hunter9000a at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 22

>Why did you change your username anyway Dr. Jamz?

Too many newbies missed the literary reference and thought I

was some august Hungarian professor. I also cracked 10K posts

and hauled a platinum star. Those, too, added gravitas that I don't look for.

I'd rather be the guy with the starchy French nom de guerre.

Hippolytea at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 23
> I'd rather be the guy with the starchy French nom> de guerre.I assumed you just wanted to advertise your new river-horse-flavored, low-calorie homebrew.
jverda at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 24
>assumed you just wanted to advertise your new river-horse-flavored, low-calorie homebrew.Less filly?
Hippolytea at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 25

> >Why did you change your username anyway Dr. Jamz?

>

> Too many newbies missed the literary reference and

> thought I

> was some august Hungarian professor. I also cracked

> 10K posts

> and hauled a platinum star. Those, too, added

> gravitas that I don't look for.

> I'd rather be the guy with the starchy French nom

> de guerre.

I always pictured you as a nerdy dj, mainly because I always read it as Jamz. It was quite entertaining.

hunter9000a at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 26
>I always pictured you as a nerdy dj, mainly because I always read it as Jamz. It was quite entertaining.it's hard to convey what a ringer you are for Arnold Schwarzenegger in his prime, using just a user name.
Hippolytea at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 27

> >I always pictured you as a nerdy dj, mainly because

> I always read it as Jamz. It was quite entertaining.

>

> it's hard to convey what a ringer you are for Arnold

> Schwarzenegger in his prime, using just a user name.

You don't feel that hippo does enough justice ? ;-P

Aknibbsa at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 28
> You don't feel that hippo does enough justice ? ;-PHippolyte was an Amazon queen whose girdle is the object of one of the labours of Heracles. That's ber-macho, right?
Hippolytea at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 29

> > You don't feel that hippo does enough justice ?

> ;-P

>

> Hippolyte was an Amazon queen whose girdle is the

> object of one of the labours of Heracles. That's

> ber-macho, right?

Oh, indeed. Nothing says tough-guy like the words "queen" and "girdle" in close proximity.

jverda at 2007-7-21 22:19:14 > top of Java-index,Java Essentials,Java Programming...
# 30

>

> No doubt I'll kick myself. Note - no mutli-threading

> going on here and this is 1.4 so no autoboxing.

So was this resolved?

If not..

- Threading issue. Something is changing it to null

- Hotspot compiler issue. In this case you have to reformat the code.

jschella at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 31

Just a shot in the dark, but,

wrapping the line executed after the (==null) check in brackets?

if (lineTotal == null)

{

System.out.println("lineTotal==null, creating new one");

lineTotal = new Money(tot);

}

else if (Math.abs(lineTotal.longValue() - tot) > 0) { // < null pointer

overrides |= 1;

wildTotal = Math.abs(lineTotal.longValue() - tot) > 2;

ALSO... As written, if lineTotal==null, it will create a new one, but not execute the "else if" part on it...is that what you intended?

xiarcela at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 32

> > Anyway, stupid question: can you use a debugger?

>

> Yes, but it doesn't seem to happen under the

> debugger.

The last time that happend to me, there were some old class files lying around that got loaded instead of the newly compiled ones, which messed up step-by-step debugging.

> >And

> > did you do a re-build, just for the sake of it?

>

> I did a make, but you're right, a rebuild might be

> worth a try.

Which did fix the issue for me, so my money's on the clean rebuild.

thomas.behra at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 33

I tried the rebuild. Still happening (and getting beyond a joke). I'll probably try decompiling, but it's a fair sized class to wade through (and this is one of the larger methods).

No, it's not a static field or anything (though it's in an inner class). I'm reading invoice lines in using a template, so that various fields may, or may not, be explicitly given. Some fields (line the line total) can be calculated if not supplied, but must be accepted "as is" if supplied.

This processing is a straight, IO bound scan. No threading.

malcolmmca at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 34
What does your Money.longValue() method look like? Is it derived?
quittea at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 35

> What does your Money.longValue() method look like? Is

> it derived?

Couldn't be much simpler:

public long longValue()

{

return pence;

}

(Money extends Number, by the way).

malcolmmca at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 36
Hmm, have you tried switching to another debugger? Maybe one of the debuggers in Netbeans, Eclipse, Idea, etc are more helpful than your JDeveloper? And how about testing on different JVM versions?
thomas.behra at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 37
What happens if you do a System.out.println() for "lineTotal" one line before?
quittea at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 38

> It's a long shot, but lineTotal isn't static by any

> chance is it? I remember DrLaszlo posting something

> about static variables not being recompiled unless

> you clean and rebuild? Or something? Maybe it was

> static final variables. Does anyone know what I'm

> talking about?

Static imports?

CeciNEstPasUnProgrammeura at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...
# 39

Oh No! It's gone away! I hate it when that happens.

Unfortunately this is an ADF based web-app, so nothing but Oracle environments will touch it with a barge pole.

I noticed that the JVM option was set to -ojvm so, on the basis that it's usually Oracle's fault, I changed the jvm option to -server. The problem didn't happen. "Aha!" I thought, until I switched it back to -ojvm to confirm and guess what? It still doesn't happen.

Bangs head on desk

Of course, this problem will come back as soon as I make my changes live.

malcolmmca at 2007-7-21 22:19:19 > top of Java-index,Java Essentials,Java Programming...