You need some error checking in your code so it doesn’t crash:
if (Trace(HitActor, this, StartofTrace, EndofTrace))
{
SelectedActor = HitActor.GetActor();
if(SelectedActor) { // you don't necessarily hit an actor
APickUp* TestPickup = Cast<APickUp>(SelectedActor);
if(TestPickup) { // Actor hit is not necessarily of type 'APickUp' or derived from it
AFlashLightBattery* const FlashLightBatteryPU = Cast<AFlashLightBattery>(SelectedActor);
if(FlashLightBatteryPU) { // 'APickUp' might not be a 'AFlashlightBattery'
if (!TestPickup->IsPendingKill() && TestPickup->IsActive())
{
TestPickup->OnPickedUp();
TestPickup->SetActive(false);
FlashLightBatteryPU->UpdateFlashLightBatteryLevel(this);
}
}
}
}
}