Error: Attempt to read property “ID” on null

Where did you put that function ? that’s better call that it inside the action, the error probably coming from missing variable.

add_action('init','newFunction');
function newFunction(){
 global $wp_query;
 // Do your code here
}