:A null object was passed as a world context object to UEngine::GetWorldFromContextObject()

I have an ARPGCharacter.cpp class:
// Sets default values
AARPGCharacter::AARPGCharacter()
{
// Set this character to call Tick() every frame. You can turn this off to improve performance if you don’t need it.
PrimaryActorTick.bCanEverTick = true;

SteelIronComponent = CreateDefaultSubobject<USteelIronComponent>(TEXT("Steel Iron component"));
AddOwnedComponent(SteelIronComponent);

}

to which I’m trying to add a component.cpp that inherits from a different class;

// Sets default values for this component’s properties
USteelIronComponent::USteelIronComponent()
{
// Set this component to be initialized when the game starts, and to be ticked every frame. You can turn these features
// off to improve performance if you don’t need them.

PrimaryComponentTick.bCanEverTick = true;
OwnerPawn = Cast<AARPGCharacter>(GetOwner());
//UE_LOG(LogTemp, Warning, TEXT(" %s"), *OwnerPawn->GetName());

}

component.h:

class AARPGCharacter* OwnerPawn;

I get the warning message:A null object was passed as a world context object to UEngine::GetWorldFromContextObject().

When I try to access GetOwner() and cast it to AARPGCharacter, it returns a nullptr.

A component will not have an owner in the constructor. You probably need to override BeginPlay and assign to OwnerPawn there.