Added support for declared SQL query
Fix null pointer exceptions