- Home /
Enumeration Equals Method Causing : NullReferenceException: Object reference not set to an instance of an object
I am trying to compare 2 enums with the equals method, as the ''=='' would not work for me and I read online it was the wrong way to do it. So I've tried a few things. First off the main snippet of the code, the error being thrown on the first line, where I try the equals method.
case BaseAbility.AbilityTypes.FLAME:
if (enemy.PlayerClass.CharacterType.Equals(BaseCharacterClass.CharacterTypes.LEAF)) {
Debug.Log("FLAME V LEAF SPECIAL MODIFIER");
modifierValue = (int)(GameInformation.Special * playerSpecialModifier);
} else if (enemy.PlayerClass.CharacterType.Equals(BaseCharacterClass.CharacterTypes.LEAF)) {
Debug.Log("FLAME V RIVER SPECIAL MODIFIER");
modifierValue = (-(int)(GameInformation.Special * playerSpecialModifier));
} else {
Debug.Log("MODIFER WAS NOT CALCULATED BECAUSE THERE WAS NO TYPE ADVANTAGE");
modifierValue = 0;
}
break;
So I created a separate class just for enumerations, to compare from that. In the same way though. I can post any other info if needed. Really would appreciate help with this thank you so much in advance.
Answer by Dave-Carlile · Jul 16, 2015 at 12:43 PM
Not sure where you read that comparing enums with == was wrong, because it isn't...
if (characterType == CharacterTypes.Leaf)...
is perfectly acceptable.
If you're getting a null reference exception then it's something else. Enums aren't references so can't give you a null reference exception. So either enemy
is null, or enemy.PlayerClass
, etc. Debug.Log
is your friend here. Add in debug statements like if (enemy == null) Debug.Log("Enemy is null")
. Add those for any reference you're, um, referencing.
I did some debugs, the enemy exists, I knew that before though. There is too much code to really be pasting it all here. Was hoping someone would spot it there, as the error is pointing to the first if statement line. Everything else exists and is there. I cant figure it out :/
I believe you must have misunderstood. The only things on that first line of code that can give you a null reference exception are the references to classes: enemy, enemy.PlayerClass (probably, don't know how that's defined but I assume that's an instance of a class).
How is CharacterType
defined. It's an enum right? If so then it's not possible for it to not exist.
Hard to say without seeing the code. Debug.Log
. Seriously, just do it :) Either that or use the debugger and set breakpoints and exa$$anonymous$$e the variables. You'll quickly figure out which one is null. I guarantee it isn't the enums.
@SmokingAces207: You have to learn to interpret the stacktrace correctly. If the "Equals" method causes a nullreference exception, it would be in the stacktrace. However it isn't. The top item is the innermost method where the actual exception occurs.
Like others have already mentioned, if the line the top most entry referres to is the line you have mentioned, it can only be either:
"enemy" is null
or "enemy.PlayerClass" is null
You said that you know the "enemy exists". The existance of an object is irrelevant. The question is whether your "enemy" variable hold a valid reference to that said enemy object or not.
From the stacktrace we can see that the state machine Update method passes an "abillity" reference to "CalculateTotalPlayerDamage" which most likely get passed all the way down.
However "CalculateAbility$$anonymous$$odifier" requires an additional "BasePlayer" reference. That means inside "CalculateAbiltiyDamage" where you call "CalculateAbility$$anonymous$$odifier" you have to pass a reference to such a BasePlayer object. This reference has to be obtained inside "CalculateAbiltiyDamage" in one or the other way. So you either use a search function (GameObject.Find and the like) or some kind of singleton pattern.
Since we don't know how your actual code looks like this is a dead-end for us.
ps: The stacktrace should have been the first thing in your question since you have interpreted it completely wrong. As Dave already said, using the == operator is perfectly fine and Equals (of a value type) can't possible cause a null-ref-exception.
edit
It's of course possible that your Ability reference got set to null on the way... $$anonymous$$aybe an "as-cast" somewhere? That's why you should never use an "as-cast" unless you check the result right away. An as-cast hides a conceptual problem behind a null value which might cause a problem(NRE) at a completely different place.
Your answer
Follow this Question
Related Questions
access enum values 0 Answers
compare Attack and Defence attribute 1 Answer
Unexplanable NullReferenceException Error 2 Answers
bool to true from a list comparison. 1 Answer