Have you tried debugging the code by adding some println()s to show what is happening?