- Home /
Check if Array.GetValue(i) is null
I am trying to make sure that a public array field in a script is not empty and contains non-null elements. For this I am using C# reflection. I get the Array object, then run thru it's elements and check if they are null. Surprisingly, this doesn't work. When I step thru with the Mono debugger, the obj is shown to have value of null in Locals, however, the null == obj check always fails! The interesting part is that obj is actually shown as (null), but still with an arrow to the left, when I click on the arrow, the arrow just disappears.
private void CheckArray(FieldInfo field)
{
Array array = field.GetValue(this) as Array;
if (array.Length == 0)
{
if (!Attribute.IsDefined(field, typeof(CanBeEmpty)))
Debug.LogError(field.Name + " array empty in " + PrintScriptLocation() + ". Make sure you have filled the array in Inspector");
}
for(int i = 0; i < array.Length; ++i)
{
object obj = array.GetValue(i);
Debug.Log("Array value: " + obj);
if (null == obj)
{
if (!Attribute.IsDefined(field, typeof(CanHaveNullElements)))
Debug.LogError("A null element found in array " + field.Name + " in " + PrintScriptLocation() + ". Make sure you set up the links to all array elemens in Inspector");
}
else
{
int a = 5; // always lands here, even if obj was shown as (null) in Locals
}
}
}
The same happens when I use fast enumeration like foreach (object element in array){..} . Even though the element is null, the check null == element always fails.
I am not an expert C#, but guess is that the object is actually not null, it's a pointer to some place which contains null itself. But why would Array.GetValue wrap the null in another object? Or is this a bug of Mono? Or C# script of Unity3d? Am I doing something wrong here?
Answer by MarkMizuguchi · Jan 03, 2013 at 07:43 PM
I ran into this issue as well. When using standard iteration techniques and search methods the test against null always returns false as you report. UnityEditor.ArrayUtility.IndexOf() behaves as expected though. So my workaround is:
static bool ContainsNull<T>(T[] array)
where T: class
{
int nullIndex = ArrayUtility.IndexOf(array, null);
return nullIndex != -1;
}
Under the hood IndexOf() is implemented as:
public static int IndexOf(T[] array, T value)
{
List list = new List(array);
return list.IndexOf(value);
}
The conversion to a List is far from efficient which is why I suspect it is part of UnityEditor and not UnityEngine.
Great, thanks. I've also seen Array.IndexOf(). Is it's implementation the same as ArrayUtility.IndexOf?
Array.IndexOf() has the same problem that you encountered and will not find the null values so it will always return -1 even if the array appears to contain nulls. For the exact implementation of Array.IndexOf() you can always download and exa$$anonymous$$e the $$anonymous$$ono source. If you need this functionality in game and not in just in the editor, simply copy & paste the IndexOf() implementation above into your own code.
Your answer
Follow this Question
Related Questions
Reorder array with nulls at the end 1 Answer
Multiple Cars not working 1 Answer
Array member treated as null even though it is not. 1 Answer
Distribute terrain in zones 3 Answers
Access if there are items in an array? 2 Answers