Error Handling: nullptr on PhysicsHandle

Hi,
I just want to show you my solution for nullptr on the PhysicsHandle:

	PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if(PhysicsHandle)
	{
		//PhysicsHandle found
	}
	else
	{
		UE_LOG(LogTemp, Error, TEXT("GRABBER OFFLINE. PhysicsHandleComponent not found on %s"), *(GetOwner()->GetName()));
		this->DestroyComponent(); //Stop Grabber from working by removing it.
	}
	
	
}

I think this is a propper way to ensure, that a crash will not happen, even with future code added.

Cheers

1 Like

This will work. Sometimes it’s better to be more explicit however so code is easier to follow.
e.g.

	PhysicsHandle = GetOwner()->FindComponentByClass<UPhysicsHandleComponent>();
	if(PhysicsHandle==nullptr)
	{
		UE_LOG(LogTemp, Error, TEXT("GRABBER OFFLINE. PhysicsHandleComponent not found on %s"), *(GetOwner()->GetName()));
		this->DestroyComponent(); //Stop Grabber from working by removing it.
	}

I hope this helps. Either way works.

1 Like

You are right, I posted this before we did

if(!PhysicsHandle)
	{
		UE_LOG(LogTemp, Error, TEXT("GRABBER OFFLINE. PhysicsHandleComponent not found on %s"), *(GetOwner()->GetName()));
		this->DestroyComponent(); //Stop Grabber from working by removing it.
	}

At that time, I did not know, if we put sth. in the //PhysicsHandle found - Part.

1 Like

Privacy & Terms