check dynamic casts for null

This probably isn't an issue, but it was spotted by coverity
and its easy to fixup. Who knows, someone might have accidentally
broken this in the future somehow.