Fix null pointer errors.

Imported from upstream commit 7a9d59c148b773f59a41f8697eeecf369a0974c2