How to use AddImpulse

I cannot figure out how do i use AddImpulse. Iam trying to make a simple roll a ball game but it keeps crashing when i press play. What am i doing wrong?

This is my cpp file:

// Fill out your copyright notice in the Description page of Project Settings.

#include "RollaBall.h"
#include "BallPlayer.h"


// Sets default values
ABallPlayer::ABallPlayer()
{
 	// Set this pawn to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
	USphereComponent* sphere = FindComponentByClass<USphereComponent>();
}

//Input function
void ABallPlayer::Move(float AxisUpDown, float AxisLeftRight) 
{
	Movementinput.Y = AxisUpDown;
	Movementinput.X = AxisLeftRight;
	sphere->AddImpulse(FVector(Movementinput.X, Movementinput.Y,0.0f));
}

// Called when the game starts or when spawned
void ABallPlayer::BeginPlay()
{
	Super::BeginPlay();
	
}

// Called every frame
void ABallPlayer::Tick( float DeltaTime )
{
	Super::Tick( DeltaTime );
	Move(Movementinput.Y,Movementinput.X);
}

// Called to bind functionality to input
void ABallPlayer::SetupPlayerInputComponent(class UInputComponent* InputComponent)
{
	Super::SetupPlayerInputComponent(InputComponent);

}

Hi,

I think this is because you are not initializing sphere correctly. As I understand it, FindComponentByClass is for returning an existing component of your actor, not initialization.

Instead, you’ll want to use a constructor that takes an FObjectInitializer as an argument, and initialize the sphere with FObjectInitializer::CreateDefaultSubobject, like so:

ABallPlayer::ABallPlayer(const FObjectInitializer& ObjectInitializer)
: Super(ObjectInitializer)
{
    	 sphere = ObjectInitializer.CreateDefaultSubobject<USphereComponent>(this, TEXT("sphere"));
}