Check if pointer is null before using it
(cherry picked from commit c01502c9f4450f05853f2d7337bf2cfdd5cd566c)