Fix potential NPE when preparing query

JAVA-172 #fixes