Fix potential null pointer dereference.

Fixes #2392 properly.