Crashes when calling GEngine->GameViewport when GEngine is not null

Hi, learning unreal here =)

I created an empty project added a character as a character c++ class and used that as a base class for my main character. Everything was working fine. I was playing with the engine a bit and added the following code:
if (GEngine)
{
UE_LOG(LogTemp, Log, TEXT(“GEngine exists”));
// GameViewport = GEngine->GameViewport;
// GameViewport->GetViewportSize(ViewportSize);
// UE_LOG(LogTemp, Log, TEXT(“The viewport size is %s”), *ViewportSize.ToString());
// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT(“The viewport size is %s”), *ViewportSize.ToString()));
// //UKismetSystemLibrary::PrintString(this, FString::Printf(TEXT(“The viewport size is %s”), *ViewportSize.ToString()), true, true, FLinearColor::White, 15.0f, TEXT(“None”));
//
// FVector2D MousePosition = UWidgetLayoutLibrary::GetMousePositionOnViewport(GetWorld());
// UE_LOG(LogTemp, Log, TEXT(“The MousePosition is %s”), *MousePosition.ToString());
// GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Red, FString::Printf(TEXT(“The MousePosition is %s”), *MousePosition.ToString()));
// //UKismetSystemLibrary::PrintString(this, FString::Printf(TEXT(“The MousePosition is %s”), *MousePosition.ToString()), true, true, FLinearColor::White, 15.0f, TEXT(“None”));
}

Mos of it is commented out because can’t figure out while the engine now started crashing on this call GEngine->GameViewport

Yesterday it was all working fine (albeit the AddOnScreenDebugMessage function wasn’t working)
Because I wanted to use UWidgetLayoutLibrary::GetMousePositionOnViewport added “UMG” to build.cs. Since then messages on screen stopped appearing (I think) and now it just straight up crashes everytime.

I’m new to the engine so any information will be really helpful

So in brief:

  1. Created a project an everything worked fine
  2. Printed some messages on screen using AddOnScreenDebugMessage
  3. wanted to use UWidgetLayoutLibrary::GetMousePositionOnViewport so added UMG to build.cs
  4. Around that time messages on screen stopped working so tried to print on screen using UKismetSystemLibrary::PrintString
  5. Game started crashing everytime on GEngine->GameViewport

Now, what I did is comment out everything, build and play. uncomment everything build and play and all is working now. So my question is how this makes sense? What was I doing wrong? What is a correct workflow that you know that there are actual errors or that we need to comment out stuff and try out again?

Thanks in advance!