How can I avoid an error when casting from AHUD to CustomHUD?

Hi

I am trying to cast my HUD class from AHUD class to AMyProjectHUD inside my character class. But, I am getting some errors when doing it.
Here is the code

APlayerController* MyPC = Cast<APlayerController>(Controller);
		AMyProjectHUD* MyHUD;
		if (MyPC)
		{
			MyHUD = Cast<AMyProjectHUD>(MyPC->GetHUD());
		}
		if (MyHUD)
		{
			const FVector2D Center = MyHUD->GetCenter();
            }

I am getting the following errors

1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): error C2220: warning treated as error - no ‘object’ file generated
1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): warning C4701: potentially uninitialized local variable ‘MyHUD’ used
1>d:\ue4\new folder\myproject\source\myproject\myprojectcharacter.cpp(77): warning C4703: potentially uninitialized local pointer variable ‘MyHUD’ used

The compiler is telling you that at the point that you check for MyHUD being non-null, there is a potential for it to be uninitialized. In C++ pointers that are uninitialized will be garbage stack memory and thus you could end up with a crash trying to dereference a pointer that doesn’t point to anything.

You can fix this by simply initializing MyHUD to nullptr:

AMyProjectHUD* MyHUD = nullptr;