x


What is a Null Reference Exception In Unity.

This question comes up very frequently so I decided I would create a general explanation of what a Null Reference Exception is. This isn't so much for me as it is for beginners and others new to programming or Unity.

Because the biggest problem appears to be that people new to programming or .Net don't understand what a NullReferenceException means, and it really isn't too hard to fix if you know what your looking for.

So, I'll be posting my explanation. If anyone else has a good explanation, then please post so that there can be one catch-all question for NullReferenceExceptions.

more ▼

asked Feb 15, 2011 at 08:57 PM

Peter G gravatar image

Peter G
17.7k 30 65 164

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

NullReferenceException are thrown when you try to access a reference variable that isnt referencing any object, hence it is null. So the first question, are all variables reference types?

No, in Mono, there are two kinds of variables that are used for most variables: value types and reference types. Value types store a value directly such as an int or a double or any struct such as a Vector3, or an enumeration.

Reference types, on the other hand to not directly store the object data, instead, they store a reference to an object similar to pointers in C/C++. Common reference types are classes, delegates, and strings. Reference types default to null, that is that they are not referencing any object. Hence, if you try and access the object that is being referenced and their isnt one, you will get a NullReferenceException.

[Here][1] is another Answer about null references that explains the same thing in slightly different words.

Some common examples:

var t : Transform; //t is a reference to a Transform.
//If you do not assign a Transform to it in the Inspector, then
//it will be be a null reference..

function Start () {
     t.Translate();
     //If t does not have a Transform, then you will get a NullReferenceException
}

If you try to get a component that isnt there then try to access it:

function Start () {
      var c : Light = GetComponent.<Light>();

     c.range = 10;
     //If this object doesnt have a light on it, then you will get
     //a NullReferenceException
}

accessing a GameObject that doesnt exist:

function Start () {
    var someGameObject : GameObject = GameObject.Find("AGameObjectThatDoesntExist");

    someGameObject.name = "NullReferenceException";
}

Less common, but annoying if you dont know it:

//Note this example is C#. There is no good way of doing delegates in js
public void NullDelegate (MyEmptyDelegate del) {
     del();
     //if you call a delegate that doesnt have any methods attached, you will get a NullReference Exception.
}


Fixes

Now to how to fix them: obviously the easiest way to fix them is to not have any NullReferences. But many times thats not possible so you need to have either try-catch blocks or conditionals.

For example:

var c = GetComponent.<MyComponent>();

if(c != null) {
    //Make sure we have a reference.
    //Do the rest of the stuff.
}

else {
    Debug.Log(There is no MyComponent attached to this object);
}

or:

try {
     //execute code;
}

catch (var ex : NullReferenceException) {
      //Do other stuff.
}

try/catch blocks require that you throw an exception and that does cost resources so more often than not, even though it is sometimes cleaner to read, it isnt as performance efficient.

[1]: http://answers.unity3d.com/questions/528288/null-reference-exception-what-is-it-and-why-do-i-g.html
more ▼

answered Feb 15, 2011 at 08:59 PM

Peter G gravatar image

Peter G
17.7k 30 65 164

Awesome explanation! Thank you it help me to undarstand core of my probleme

Oct 23, 2013 at 01:28 AM ScorpyX

Ty for the nice work :-)

Jan 14 at 03:53 AM Pedro Oliveira
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3454
x735
x389
x224
x30

asked: Feb 15, 2011 at 08:57 PM

Seen: 30991 times

Last Updated: Jun 20 at 10:54 AM