1

I'm relatively new to programming in general, and I'm learning C# and using it in Unity3D for game development, and I have a question about if possible, how to avoid NullReferenceExceptions in an if statement when asking if a variable is equal to something when the variable is null.

I have this if statement that checks if the gameObject that a raycast hit is equal to a certain name, and if you are to press out into the void of the skybox that object will of course return null, and this causes a NullReferenceException.

I just want to know, is it even worth trying to avoid even though it doesn't affect anything at all, it's just a bit disturbing to me, XD.

This is the if statement,

if (Input.GetButtonDown ("Fire1") && rayhit.transform.name == "Reflector") 
        {

        }
user2690614
  • 11
  • 1
  • 4
  • object.Equals(a, b) can deal correctly with nulls in the arguments. – Sander Aug 21 '13 at 13:46
  • Thanks for the answers everyone, although I didn't want to know how to do it, I'm not *that* new, I just wanted to know if I should bother or not. – user2690614 Aug 21 '13 at 13:49
  • Well, this is odd to me, simply referring to this varaible when it's null results in a NullReferenceException, meaning asking if it's equal or not equal to null results in one, I don't understand? – user2690614 Aug 21 '13 at 14:19
  • Ah, I see. If you are looking for justification, I guess I can describe what I do. I assert at the beginning of every function that the input parameters are valid (not null, right range etc.) and I just assume that class invariants are valid (e.g. if I define some UI with an OK button, I am not going to double check if the OK button really is there). That sort of stuff. Seems to work out OK. – Sander Aug 22 '13 at 04:36
  • When you do rayhit.transform.name you dereference rayhit and transform and access name - this means that rayhit and transform must be non-null but name may be null. Object.equals does of course not get you past this issue, since it only sees the last part (the name), so it does not help you here. Really, the "rayhit.transform" part is not even a part of the comparison - only the name is compared. Perhaps this confuses the subject matter? – Sander Aug 22 '13 at 04:45
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – CodeSmile Feb 16 '15 at 10:50

3 Answers3

1
String myValue= null

if(myValue != null && myValue.equal("value"))
{
 //Do something
}
Johan Nordli
  • 1,220
  • 3
  • 13
  • 26
0

you could write your condition so that it checks if the object is null first

example

if(myvar != null && /* your condition */){
    //do stuff
}

the /* your condition */ part will not be executed if the first condition fails

amdorra
  • 1,536
  • 9
  • 18
0

I think you should avoid exceptions when you can. It is faster, because exceptions take a lot of process time and it is easier to read code, for me at least, when you have an if statement instead of a try-catch statement.

For you example you can do if(skybox != null) before you dive into this equal method to avoid the exception.

Here you can read about "why you should avoid exceptions". They complicate your thinking, complicate the code, and make debugging a nightmare.

Bart
  • 19,692
  • 7
  • 68
  • 77
No Idea For Name
  • 11,411
  • 10
  • 42
  • 70