The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

wim.deblauwe (17) [Avatar] Offline
#1
Hi,

building on the examples in listing 4.1 (range declarations), I slightly modified the example to this:

def today = new Date()
def yesterday = today - 1

assert (yesterday..today).size() == 2
def x = (yesterday..today)
println x.size()
println (yesterday..today).size()

Why do I get a NullPointerException on the last line?
Mittie (397) [Avatar] Offline
#2
Re: Why do I get a NPE for this?
Hi Wim,

this is because of the associativity of parens and dots. In
> println (yesterday..today).size()
you have chosen to omit the parens around the println args. You wanted

println ( (yesterday..today).size() )

but you did really

println ( (yesterday..today) ) .size()

and since println returns null, you end up with null.size(). Therefore the NPE.

cheers
Dierk
wim.deblauwe (17) [Avatar] Offline
#3
Re: Why do I get a NPE for this?
Aah...

Thanks for the clarification!

regards,

Wim