Crash when Iterating through a TArray whith for loop

I have been following an Unreal Engine course on Udemy and at a point in the course, we were required to get the mass of all the actors that overlap with a certain Trigger Volume(namely PressurePlate). We achieved this by using
GetOverlappingActors() member of TriggerVolume and then iterating through each item to get mass.

But when I try to iterate through the members of the TArray(out arg of GetOverlappingActors), the editor crashes on play. The log says that there is an Unhandled Exception is the line with the for loop.

The function Defination -:

float UOpenDoor::GetTotalMassOfActorsOnPlate()


    float TotalMass = 60.0f;
     TArray<AActor*> OverlappingActors;

// Find all the overlaping actors
PressurePlate->GetOverlappingActors(OUT OverlappingActors);

// Iterate through their masses
for (const auto& Actor : OverlappingActors)
	TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();

	UE_LOG(LogTemp, Warning, TEXT("%s is on pressure plate."), *Actor->GetName())

return TotalMass;


The Function call -:

 if (GetTotalMassOfActorsOnPlate() > MassThreshold) 

Any help would be appreciated.

The crash report-:

EDIT2: Spelling errors.

The crash stack trace would make it way easier to help you out.

Atm random best guesses

Actor->FindComponentByClass() is null
for whatever strange reason the actor has not Primitive component and you try to GetMass() on a nullptr object.

const auto& Actor is of type AActor* const& which is weird but not a crash reason

I attached a text file containing everything is the Unreal Crash Reporter.

Without knowing exactly what is on line 75 of opendoor.cpp, I’m going to guess it’s this one:

 TotalMass += Actor->FindComponentByClass<UPrimitiveComponent>()->GetMass();

Here you’re assuming that every overlapping actor has a UPrimitiveComponent. If an actor doesn’t have a primitive component (which is quite possible) then you’ll be trying to GetMass from a null pointer, which would cause the error you see.

So, to fix you could check that the component exists before attempting to GetMass. Quick way is to do something like:

UPrimitiveComponent* comp =  Actor->FindComponentByClass<UPrimitiveComponent>();
    if(comp != nullptr)
       TotalMass += comp->GetMass();

But personally I’d look at trying a different method which is a bit more optimal. FindComponentByClass can be expensive to run multiple times per tick so perhaps using an Interface or a custom Actor class which you can query / filter more directly would be beneficial. It depends on what you’re trying to do though.

Line 75 of opendoor.cpp is the start of the for-loop. And Yes, I did try putting NULL value checkers wherever I could (on every step basically) but the SAME problem persists.

Just try commenting out the line I thought was line 75 (where you increase the value of TotalMass) and see if you still get the error.

if the error is cleared, you know the issue is with a null actor or a null return from FindComponentByClass so re-implement your null safety checks and ensure you’re catching every case.

If you still get the error then check that GetOverlapping Actors is working as expected (again, you could just comment it out for now). I never use OUT in that way but I assume its just a convinient way to pass by reference