Add checks for null after dynamic_casts<>.

These are not expected to ever fail,
but this change stops Coverity Scan from warning about a possible
"Null pointer dereference".
Actually, there are probably many more possible null pointer
dereferences, but dynamic_cast<> is the only one that Coverity
can be sure is capable of producing a null.