Fix NullReferenceException in type analysis.

Closes #393, #386, #403.