Fix NPE and validation query