This tutorial is misleading. We cannot attach components to uninitialized AActor::RootComponent

Misleading QuickStart Tutorial

VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
VisualMesh->SetupAttachment(RootComponent);

static ConstructorHelpers::FObjectFinder<UStaticMesh> CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

if (CubeVisualAsset.Succeeded())
{
    VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
    VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
}

How can we attach components to RootComponent that is still nullptr?

Hello pstricks.fans,

Thank you for bringing this to our attention. We have logged a ticket with our Documentation Team to get this page updated, in the meantime, as a workaround you can declare the following:

if (USceneComponent* ExistingRootComponent = GetRootComponent())
{
     VisualMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("Mesh"));
     VisualMesh->SetupAttachment(RootComponent);

     static ConstructorHelpers::FObjectFinder<UStaticMesh> 
CubeVisualAsset(TEXT("/Game/StarterContent/Shapes/Shape_Cube.Shape_Cube"));

    if (CubeVisualAsset.Succeeded())
    {
      VisualMesh->SetStaticMesh(CubeVisualAsset.Object);
      VisualMesh->SetRelativeLocation(FVector(0.0f, 0.0f, 0.0f));
    }

}

Thanks!