Java cleanup in v2.1-dev

I found some performance, security (possible NullPointerException) and style warnings when browsing the code using Eclipse 4.2.1 and IntelliJ Idea 12. I fixed some to keep the changes as small as possible.