Download

TMap null exception?

say… if I use FindRef to find specific value by key, but if it cannot find it?

i did it as



if (theTMap.FindRef() == NULL) return NULL;
return theTMap.FindRef();


but the engine crahses if it cannot find it relevant value.

any suggestion?

Don’t use FindRef. Since there is no such thing as a null reference, that method has to assert on failure. If there is any chance the element won’t be found, just use the Find method instead and check result for nullptr.

Thanks. i will go check for Nullptr… what is NULL pointer in Unreal Term? ACCESS_NONE?

There is no UE4 specific null value. You can use NULL but using the C++ nullptr is preferable.

Oh I see… thanks. i will keep that in mind

Tmap cant use == NULL or nullptr , how to check it empty? if it is null , use tmap.null() have exception…

i had test successful, can use &tmap != nullptr , can do it

What is the type of “theTMap” that you are checking? You are using it as a reference, but checking it as a pointer. As Kamrann said, one of the big differences between a pointer and reference is that a reference can never be NULL.




// This function takes in a TMap reference. A Reference implies that my function doesn't need to worry if the object being passed in is null or not.
bool IsMyMapEmpty(TMap<int32, int32>& MyMap) const
{
   return MyMap.Num() == 0;
}

// This function takes in a TMap pointer. A Pointer implies that my function does need to check the object for null to be safe.
bool IsMyMapEmpty(TMap<int32, int32>* MyMap) const
{
   return MyMap != nullptr ? MyMap->Num() == 0 : true; // If you don't know about the Ternary operator (?), all this line is doing is checking the condition (MyMap != nullptr) and if its true it returns the result on the left (MyMap->Num() == 0), otherwise it returns the result on the right (false).
}



If your “theTMap” variable can indeed be Null, I would suggest changing your code to use pointers just for clarity sake.