Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by SmokingAces207 · Jul 16, 2015 at 12:39 PM · nullreferenceexceptionenumcomparenullreferenceequal

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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.

Comment
Add comment · Show 13 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SmokingAces207 · Jul 16, 2015 at 12:52 PM 0
Share

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 :/

avatar image Dave-Carlile · Jul 16, 2015 at 12:56 PM 1
Share

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).

avatar image Dave-Carlile · Jul 16, 2015 at 01:12 PM 1
Share

How is CharacterType defined. It's an enum right? If so then it's not possible for it to not exist.

avatar image Dave-Carlile · Jul 16, 2015 at 01:29 PM 1
Share

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.

avatar image Bunny83 · Jul 16, 2015 at 01:53 PM 2
Share

@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.

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

access enum values 0 Answers

compare Attack and Defence attribute 1 Answer

i have written this code to spawn a segments of enemies i don't get errors at all but it didn't work .. what should i do ? 2 Answers

Unexplanable NullReferenceException Error 2 Answers

bool to true from a list comparison. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges