I am trying to call the line trace function from the gun inside the character MCVE.[EDITED]

void ATheLeadGiverCharacter::StopFiring()
{
AM4A1* M4A1 = Cast(GetMesh());
if (M4A1)
{
M4A1->StopFiring();
}
}
void ATheLeadGiverCharacter::Fire()
{
AM4A1* M4A1 = Cast(GetMesh());
if (M4A1)
{
M4A1->Fire();
}

    }
    void ATheLeadGiverCharacter::BeginTouch(const ETouchIndex::Type FingerIndex, const FVector Location)
    {
    	TouchItem.bIsPressed = true;
    	TouchItem.FingerIndex = FingerIndex;
    	TouchItem.Location = Location;
    	TouchItem.bMoved = false;
    	if (TouchItem.bIsPressed == true)
    	{
    		return;
    	}
    	if ((FingerIndex == TouchItem.FingerIndex) && (TouchItem.bMoved == false))
    	{
    		Fire();
    		//FireShot();
    	}
    }
    Here is where I am calling the functions within the gun. 
    Below is where I am placing the linetrace functionality.
    void AM4A1::FireShot()
    {
    	ATheLeadGiverCharacter* Player = Cast<ATheLeadGiverCharacter>(UGameplayStatics::GetPlayerCharacter(this, 0));
    	//Here is where I get stuck I don't know where to cast to the skeletal mesh inside of the character or should I just use the gun's skeletal mesh. 
    	FHitResult* HitResult = new FHitResult();
    	
    	FHitResult Hit;
    
    	const float WeaponRange = 20000.0f;
    	FVector CameraForward = FVector(Player->AssaultRifle->GetForwardVector());
    	const FVector StartTrace = Player->AssaultRifle->GetSocketLocation(FName("Muzzle1"));
    	const FVector EndTrace = (Player->AssaultRifle->GetForwardVector() * WeaponRange);
    	FCollisionQueryParams QueryParams = FCollisionQueryParams(SCENE_QUERY_STAT(WeaponTrace), false, this);
    	FCollisionQueryParams* TraceParams = new FCollisionQueryParams();
    	
    	if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
    	{
    		
    		if (Hit.GetActor())
    		{
    
    
    		}
    	}
    
    	if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
    	{
    		if (ImpactParticles)
    		{
    			UGameplayStatics::SpawnEmitterAtLocation(GetWorld(), ImpactParticles, FTransform(Hit.ImpactNormal.Rotation(), Hit.ImpactPoint));
    		}
    	}
    	if (GetWorld()->LineTraceSingleByChannel(Hit, StartTrace, EndTrace, ECC_Visibility, QueryParams))
    	{
    		if (MuzzleFlash)
    		{
    			UGameplayStatics::SpawnEmitterAttached(MuzzleFlash, M4A1Mesh, (FName("Muzzle1")));
    		}
    	}
    
    
    }
    void AM4A1::Fire()
    {
    	FireShot();
    	GetWorldTimerManager().SetTimer(FireTimer, this, &AM4A1::FireShot, ShotDelay, true);
    //Here I am firing the gun itself. 
    }
    void AM4A1::StopFiring()
    {
    	GetWorldTimerManager().ClearTimer(FireTimer);
    //Here I am stopping the automatic timer. 
    }
    
    //I edited the code so that I could make the problem a little easier to solve and more concise picking up the gun is no longer a concern I just want to call the line trace function from the gun within the character. Picking up the gun is a secondary objective and is unnecessary to achieve this goal. Please forgive the edits.

I am trying to attach the line trace to a skeletal mesh that is not attached to the character.

You call GetMesh which gives you the skeletalMeshComponent, but then you try to cast that to an actor (Am4a1) which can’t work, so you get a nullptr.
And then you call a function on that nullptr in the next line (m4a1->m4a1Mesh.
That is causing the crash.
That said I don’t completely get what you are trying to achieve. Also; where exactly is this code located? In your character?.

The code is located in the character. I just wanted to make the line trace specific to that mesh. I am casting to the M4A1 mesh so that I can socket the line trace to that M4A1’s socket muzzle. The reason why I am doing this is so that I can pickup the gun during runtime instead of having to create a skeletal mesh component. I mainly don’t what to have to use inheritance. On the components section of the viewport.

How do I remove the nullptr? Please elaborate.

How would you do it?

Ok first of all
Calling GetMesh on your character as you do will get you the SkeletalMeshComponent of your character which is used to render the mesh of your character (or in case of an fps his hands)

Could you show me how picking up a weapon works in your code?
Also why is your weapon not attached to your character if you can use it?
You need to have that weapon actor instantiated and referenced somewhere if you want to render and somehow use it.
Maybe take a look at unreals Shootergame example to see how its done there

I am using a linetrace to destroy the object in play and then I am spawning the weapon and attaching to the hand socket.

The reason why I am trying to cast to the gun pickup is because I want to attach the line trace and muzzle flash to the weapon that is on the ground not the weapon that is attached to the character. I don’t know if I should create the line trace code within the gun itself then attach the line trace to the gunPickup’s mesh rather… But I don’t believe I can cast to a function inside of the gun itself in order to fire the line trace code. Believe me I know what you are saying though.

Are you saying I should create the line trace code inside of the gun itself then call the linetrace function from the character?

I changed the premise to the MCVE of the problem.

I used getplayercharacter(this, 0) instead it worked like a charm as a parameter to use inside of the cast get mesh is faulty at times

Using GetPlayerCharacter(this, 0 ) instead of getmesh() works like a charm when passing parameters inside of the cast. In some instances.