AddMappingContext not working in C++

if (APlayerController* PlayerController = Cast<APlayerController>(GetController()))
	{
		if (UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(PlayerController->GetLocalPlayer()))
		{
			Subsystem->AddMappingContext(DefaultMappingContext, 0);
		}
	}

This code is called in BeginPlay but it does nothing. If I do the exact same thing in the Child Blueprint of the class, it suddenly works. I did all the necessary steps to include the enhanced input system, but it won’t work from C++.

Is DefaultMappingContext valid in the class where it doesn’t seem to be working?
I tried it out on an ACharacter derived class and it registers the input.

I did some more testing and the problem is that the variable Controller, which is derived by APawn, is not valid. Because of that the first if check fails. I find this really odd because it is valid in BeginPlay of the Blueprint but not in C++.