Check if main window exists before calling getWindowIndex( 0 ).  

Fixes #569.