Check return value to avoid null pointer dereference