- Home /
The question is answered, right answer was accepted
Null Reference Exception, what is it and why do I get it?
Guys, I have a NullReferenceException. How do I fix it?
Answer by Jamora · Sep 01, 2013 at 03:05 PM
A Null Reference Exception is thrown when you're trying to use a reference that points to null.
Cool. But what does that mean?
There are three things you need to know if you want to fix null reference exceptions.
What is a reference?
How are references used in code?
What is null?
What is a reference?
A reference is simply an address in memory to where the data you're trying to access is located. If you want a more in-depth explanation, read the sections Reference Types and the Heap and Creating a Reference of the brilliant Memory Management UnityGems article. I suggest you read the whole article (and peruse the whole site) if you're interested in programming.
How are references used in code?
Alright then, the concept of reference was simple enough, but how are they actually used when programming? How do you know if you're using a reference? The answer again is rather simple: Every time you have a dot (a period, ".") in your code, a big red sign should appear in your head that asks: "Can this be null?".
Our colleages at Stack Overflow have (apparently) had their share of null reference questions, and here is their answer on fixing null references: "What is a NullReferenceException in .NET and how do I fix it?". He gives code examples in C#. If you can't comprehend C# because it's not UnityScript, here's a good resource that allows you to translate between the two: Differences in JS and C#.
What is null?
Null is a special address in memory that, to the computer, means nothing, zero, zip, nada or otherwise empty. So if you have a reference to null (called a null pointer) and try to use it, the compiler has no choice but to throw an exception because there is nothing to use.
The hunt begins...
Because there is no magical fix to a null reference exception, each case must be handled on its own. When hunting for null reference exceptions, the error message gives the line where it is thrown. Study the line while keeping these three things in mind:
Every dot in your code is a potential null reference error.
Every reference type (another page explaining the difference of value vs. reference types) variable you access is a potential null reference error. Value types are never null.
Some methods (GetComponent probably being one of the most infamous) return null if they 'fail'.
Common causes for Null Reference Exceptions
You have not set the variable in the inspector.
You have (accidentally) added the script to multiple GameObjects + the above.
You are trying to get a script from a GameObject which is not on that GameObject. (Using GetComponent.)
You are using GameObject.Find to find a GO by name, but a GO with that name doesn't exist.
Other Resources
After a more thorough searching of UA, I found Peter G has made a similar Question/Answer two years ago.
I'd like to add though, that most if not all of NullRef problems that are being asked are solved by taking the time to debug. ($$anonymous$$onoDev, VS)