checking for null query_id on state when initialising query.

Fixes all issues in tickets #10851 & #10859