简体   繁体   中英

How to find out which object reference is null, when an “Object reference not set to an instance of an object” exception is recorded in Crashlytics?

I have a published game with Crashlytics, that run on Android and iOS.

I have records of "Object reference not set to an instance of an object" exceptions that tell me in which method a null reference has been encountered, but not exactly which reference, or on which line this happened.

The code runs successfully in local tests, but in real usage scenarios, null references can happen.

Please note in Unity, this can mean an actual MonoBehaviour object is destroyed and no more available, so adding null checks everywhere doesn't help. I want to know exactly which object has been destroyed, so I can find out the sequence of events that leads to the exception.

I've thought about adding Crashlytics.SetCustomKey() every few lines with an updated value so I can at least guess on which line this happens, so I can make better guesses.

But is there any better strategy you would suggest?

Edit: This question has been marked as duplicate, so I'm describing why it's not a duplicate of the mentioned questions.

I already know why a NullReferenceException happens, and if it happened locally for me, I could fix it. This question is a separate question from the mentioned duplicates. The question is about a complex situation when a sequence of events lead to some objects being destroyed, when they are not expected to be destroyed.

My question is more related to better utilizing Crashlytics, than related to NullReferenceException.

To describe the situation better, this is a boards game. For each round of game, there are some pieces created on board, and when player touches the boards, then some highlight objects are created on board, so player can select where he can move a piece.

As a possible example, when a player restarts the game, or leaves the game in any situation, those objects are destroyed.

But another job may finish it's job after the refresh (could be the AI thinking) and then expects objects on board, and they are not there anymore!

I really don't know what code to share, I think I'd need to share a big share of the project. And I don't think that helps too. Because I'm not looking for direct answers to the problem. But I'm generally asking about how to better get more info about NullReferenceExceptions recorded by Crashlytics.

I've also already searched, but found no solution for my specific problem.

So considering the situation described, I know the method in which the exception happens. How do you think I can know on which line this exception happens, or which object is null, when I have the record in Crashlytics? (Besides the solution I mentioned myself)

Edit 2 : I initially thought the stacktrace might distract people form the actual problem, but looking at the comments, I think it actually helps.

This is the stacktrace:

Non-fatal Exception: java.lang.Exception: NullReferenceException : Object reference not set to an instance of an object. at OfflineGameScreen.MoveSuggestionTouched(OfflineGameScreen) at OfflineGameScreen+<MakeNextMoveByBot>d__74.MoveNext(OfflineGameScreen+<MakeNextMoveByBot>d__74) at System.Threading.ContextCallback.Invoke(System.Threading.ContextCallback) at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext) at System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner.Run(System.Runtime.CompilerServices.AsyncMethodBuilderCore+MoveNextRunner) at System.Action.Invoke(System.Action) at System.Threading.SendOrPostCallback.Invoke(System.Threading.SendOrPostCallback) at UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke(UnityEngine.UnitySynchronizationContext+WorkRequest) at UnityEngine.UnitySynchronizationContext.Exec(UnityEngine.UnitySynchronizationContext)

Don't know the details if your code of course, but you may log the InstanceID of your object on destruction, and also before where the error is thrown. That might lead you to the logic gap where the exception might occur.

Be carefull with the nullchecks , mainly in the Updates() because they are not "free", as unity has it own implementation of the == operator on dealing with the typical if (myGameObject == null) {} .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM