- Home /
Unexplainable NullReferenceException
This is the code
activeNumberOfUnits = System.Int32.Parse(editorUnits[activeClass]);
System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length - editorQPerUnit.Length);
System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length + activeNumberOfUnits);
for(int i = 0; i < activeNumberOfUnits; i++){
editorQPerUnit[i] = GUI.TextField (new Rect(Screen.width/activeNumberOfUnits*i, Screen.height/10, Screen.width/activeNumberOfUnits, Screen.height/20), editorQPerUnit[i], 2);
}
This isn't the first existence of these lines in the script. The same coding (different variables) is used before and after this error. The error is on line 5. Ive been stuck on this error all day. I looked at everything at least 10 times. I have even tried renaming all the variables that it uses. editorQPreUnit is a string array. activeNumberOfUnits is an int.
Not an expert on Unity's internals, but does the static GUI.TextField(...)
method require string text
to be non-null?
Different class here, but it feels like a pretty similar situation. If that is really the case, it's a shame that Unity's internals aren't perfor$$anonymous$$g the null check and throwing ArgumentNullException
s ins$$anonymous$$d.
It requires a string to edit, and can only edit a string, so yes
Right well that's probably your null reference. The default value for a string
is null
. When you perform the array resize operations, you're not setting anything to the new array cells. So you've got null strings in your array which you hit with the for loop.
System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length - editorQPerUnit.Length);
Is always equivalent to...
System.Array.Resize<string>(ref editorQPerUnit, 0);
And the resize operation truncates the array. Now editorQPerUnit
is an array of length 0, so when you call...
System.Array.Resize<string>(ref editorQPerUnit, editorQPerUnit.Length + activeNumberOfUnits);
You're left with an array of size activeNumberOfUnits
filled with null
. Call the for loop on that and if GUI.TextField(...)
won't take null strings, there's your problem. Did you mean to do something else with the array resizes?
Yes, editorQPerUnit
is not null. But I'm saying editorQPerUnit[i]
is.
And you call GUI.TextField (new Rect(...), *editorQPerUnit[i]*, 2);
... which is GUI.TextField (new Rect(...), *null*, 2);
for any i.
Try setting all elements of your array to string.Empty
right after you resize it the second time ins$$anonymous$$d of null
. Alternatively, just call GUI.TextField (new Rect(Screen.width/activeNumberOfUnits*i, Screen.height/10, Screen.width/activeNumberOfUnits, Screen.height/20), string.Empty, 2);
I don't really see the point of your two lines, you first set it to 0 and then to activeNumberOfUnit. :
editorQPerUnit = null;
editorQPerUnit = new string[activeNumberOfUnit];
Answer by VOTRUBEC · Feb 04, 2015 at 08:33 AM
If the array editorQPerUnit
was null, trying to find editorQPerUnit.Length
would result in an exception.
But by resizing your array upwards, you're adding null
entries into the array.
Try defining defaults for the additional editorQPerUnit items. For example:
for (int i = 0; i < editorQPerUnit.Length; i++)
{
if (editorQPerUnit[i] == null)
editorQPerUnit[i] = String.Empty;
}
Answer by karma0413 · Feb 04, 2015 at 05:23 AM
Newbie response:
It appears that one of those two variables are null. 1. activeNumberOfUnits or editorQPerUnit[]
Its really as simple as that. Try 1 line up print (active) and also editorQper.... If your error on the code line falls on that print then you know it's that variable that is giving you null.
Ideally, it would be best to work out the logic to make sure null doesnt happen in the first place. if your not sure what I mean you could put this above line 5:
if (activeNumberOfUnits == null){print ("it was the unit thingie");}
if (editorQPerUnit[i] == null){print ("it was your array");}
Is it possible for an int to be null? I think by default, its 0. Even if one of them were null, witch it was, I would have been able to see it in the inspector. Problem solved, read the comments
An int
is zero by default and can't be null
. UNLESS you define it as int? activeNumberOfUnits;
(a nullable variable). See the $$anonymous$$SDN page.
Your answer
Follow this Question
Related Questions
Make sphere shoot to Touch.position error 1 Answer
NullReferenceException: Object reference not set to an instance of an object 3 Answers
Unity Editor & iOS - Download MP4 to Streaming Assets Folder 0 Answers
How to check for null 2 Answers
I cannot understand where the Object reference is not set to an instance of an object? 1 Answer