Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null Reference After a couple of minutes into Game #58

Open
sfratini opened this issue Aug 12, 2015 · 7 comments
Open

Null Reference After a couple of minutes into Game #58

sfratini opened this issue Aug 12, 2015 · 7 comments

Comments

@sfratini
Copy link

Hi! After a couple of minutes of using the Game in Unity, the Editor shows that the console is being filled (999+) with Null references in the GoogleAnalyticsMPV3 class in just a couple of seconds. Particularly, this line:

GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));

Some debugging made me realize the instance was null, therefore the error. I solved it replacing the code with:

try {
GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));
} catch (Exception e){
}

To avoid filling the log and replace the getInstance code with this:

if (instance == null)
    instance = this;
return instance;

Before, if would just return the instance and since it was null, I was getting the error. Will this affect my reports in any way?

@cedtat
Copy link

cedtat commented Aug 23, 2015

Experiencing same issue in Unity editor after few minutes, didn't check on mobile device yet.
GameObject containing the script is still in the hierarchy with appropriate datas but the instance variable is null.

@sfratini, yes reports are affected since nothing is sent anymore after this error

@DaleEmrose
Copy link

I get this too, and using @sfratini's suggestion to replace getInstance with:

if (instance == null)
    instance = this;
return instance;

is not possible because you can't use "this" in a static method or property.

The weird thing for me is that it's works fine for the first run in the Unity editor, but subsequent runs it always fails. If I quit and reopen Unity it again works first time, but as soon as I stop and play again it always fails...

@sfratini
Copy link
Author

My bad, I posted a wrong copy/paste. Is everyone getting the error on the same line and class?

Class GoogleAnalyticsMPV3, line 144:

try {
GoogleAnalyticsV3.getInstance().StartCoroutine(this.HandleWWW(new WWW(newUrl)));
} catch (Exception e){
Debug.Log("NullPointer. Creating instance");
if (GoogleAnalyticsV3.instance == null)
GoogleAnalyticsV3.initialized = false;
}

Then, you need to change GoogleAnalyticsV3 (public and static):

public static bool initialized = false;

Ugly as hell, but well. You can ignore the previous code.
What do you guys think? This should create the instance once GA awakes since the InitializeTracker() routine checks for this value and then creates the instance.

@DaleEmrose
Copy link

It appears to work but if I untick "Dry Run" I'm getting a different error now:

Coroutine couldn't be started because the the game object 'GAv3' is inactive!

@sebastienalcamo
Copy link

The question we need to ask ourselves is "How the hell the GoogleAnalyticsV3 instance could be null ?"

The root cause of this problem is the Unity's hotswapping.
If we edit a script and if we focus in Unity editor while the application is running, there would be a process of serialization & deserialization. Any serializable data type will get back to its initial value. By contrast, any non-serializable singleton instance will be reset to null, like the GoogleAnalyticsV3 one.

If you have set GA "UncaughtExceptionReporting" to true, the plugin will try to log any exceptions thrown. After a hot swap, on the next SendGaHitWithMeasurementProtocol, there will an exception thrown (because GAv3 instance is null, NullReferenceException) that will lead to a new SendGaHitWithMeasurementProtocol that will lead to another exception, etc...

Even if you set GA "UncaughtExceptionReporting" to false, the uncaughtExceptionStackTrace will be set to an empty string after a hot swap. (Yes, this is a really weird behaviour...).

Like suggested by https://github.com/cobbpg in this github issue #69, we should replace GoogleAnalyticsV3.cs : L133
if (uncaughtExceptionStackTrace == null)
by !string.IsNullOrEmpty(uncaughtExceptionStackTrace)

This kind of scenario only happens when running the application in Unity editor.

@adamsiemion
Copy link

I have exactly the same problem, I use V4.

From my investigation it seems, when InitializeTracker is called initialized is true (even for the first time), while instance is null. The odd thing is that, initialized is initialized with false and the only place where it is set to true is inside InitializeTracker.

@Cyberben3d
Copy link

I have this problem in V3 and V4. Tested on Unity 2017.2.0.f3 and 2017.3.0.f3. The first time I hit play in the editor it works without an error. After stopping and playing again I get the NullReferenceError every time I try to send data e.g. LogScreen(string) until I close Unity and start again. Just to verify, this is an editor only issue? It will run okay on iOS and Android?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants