Issue with String.tokenize() in Groovy

Groovy (programming language)
Image via Wikipedia

Here is some Groovy code:

class Test {
String zx = new String("4, 5")
void toy() {
char xx =','
def myBits = zx.tokenize(xx)
println "first bit is ${myBits[0]}"
println "second bit is ${myBits[1]}"
}
}

Test ff = new Test()
ff.toy()

Apologies for the fomatting but hopefully you can see what this trying to do: tokenise a String with comma as the delimiter.

Run this code in the Groovy web console and it gives the output you would expect –
first bit is 4
second bit is 5

But when I run it at home it will collapse with a null pointer exception if I try to read the second object. Essentially it falls over if there is any whitespace after the comma.

This appears to be a problem with my configuration as asking on IRC only got me a reply that the person concerned could not replicate the problem and clearly it runs in the web console too.

Anyone else seen something similar/willing to test the above code fragment?

Update: I thought that maybe this was because I was using the openjdk (installed by default on amd64 boxes by Ubuntu). But the error persists with the Sun/Oracle JDK. Very strange.

2 thoughts on “Issue with String.tokenize() in Groovy

Comments are closed.