Null reference error

211 views
Skip to first unread message

Dennis O

unread,
Jan 14, 2016, 2:50:56 PM1/14/16
to Google Mobile Ads SDK Developers
After removing all the debug statement from the dummy client I seem to now get a null error for when ever
 public void GameOver()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
            shown = true;
        }
    }

"interstital.isLoaded" is called.
I tried removing admob and then reinstalling it but that did not solve the problem.
I can see my interstitial on my mobile either so really not sure what's up
This is what it says in the editor log "
Registering platform support modules:
Platform assembly: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\Boo.Lang.Compiler.dll (this message is harmless)
Platform assembly: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\Boo.Lang.dll (this message is harmless)
Platform assembly: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\Boo.Lang.Parser.dll (this message is harmless)
Platform assembly: C:\Program Files\Unity\Editor\Data\Mono\lib\mono\2.0\UnityScript.Lang.dll (this message is harmless)
Registered platform support modules in: 0.0785167s.
Native extension for WindowsStandalone target not found
Native extension for Android target not found
Load scene 'Temp/__Backupscenes/0.backup' time: 61.315346 ms 
[remote] error: Init socket failed
Unloading 13 Unused Serialized files (Serialized files now loaded: 0)
Game Over True

Unloading 0 Unused Serialized files (Serialized files now loaded: 0)
NullReferenceException: Object reference not set to an instance of an object
  at Adverts.GameOver () [0x00000] in C:\Users\user\Desktop\RBLY-Android1\RBLY-Android\RBGY\Assets\Scripts\Adverts.cs:31 
  at UclickableButton.ReadyTrue () [0x00005] in C:\Users\user\Desktop\RBLY-Android1\RBLY-Android\RBGY\Assets\Scripts\UclickableButton.cs:37 
  at ClickCounter.FixedUpdate () [0x0001c] in C:\Users\user\Desktop\RBLY-Android1\RBLY-Android\RBGY\Assets\Scripts\ClickCounter.cs:28 
 
(Filename: Assets/Scripts/Adverts.cs Line: 31)

Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 0.038006 seconds (Nothing changed)
UnloadTime: 1.244151 ms
System memory in use before: 154.0 MB.
System memory in use after: 154.1 MB.

Unloading 0 unused Assets to reduce memory usage. Loaded Objects now: 2503.
Total: 3.530728 ms (FindLiveObjects: 0.187732 ms CreateObjectMapping: 0.029354 ms MarkObjects: 3.307156 ms  DeleteObjects: 0.005461 ms)

Unloading 0 Unused Serialized files (Serialized files now loaded: 0)
System memory in use before: 154.0 MB.
System memory in use after: 154.1 MB.

Unloading 0 unused Assets to reduce memory usage. Loaded Objects now: 2503.
Total: 3.357332 ms (FindLiveObjects: 0.181929 ms CreateObjectMapping: 0.028671 ms MarkObjects: 3.136832 ms  DeleteObjects: 0.008533 ms)

Load scene 'Temp/__Backupscenes/0.backup' time: 65.969391 ms 
Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 0.038898 seconds (Nothing changed)
Ignoring 'C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/Advertisements/UnityEngine.Advertisements.dll' because we're compiling for Editor
Ignoring 'C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityAnalytics/UnityEngine.Analytics.dll' because we're compiling for Editor
Ignoring 'C:/Program Files/Unity/Editor/Data/UnityExtensions/Unity/UnityPurchasing/UnityEngine.Purchasing.dll' because we're compiling for Editor
EditorHelper: Attempting to use Visual Studio.
EditorHelper: Checking for Visual Studio installations.
EditorHelper: Searching for Visual Studio 2008 installation using the VS90COMNTOOLS environment variable.
EditorHelper: No Visual Studio 2008 installation found.
EditorHelper: Searching for Visual Studio 2010 installation using the VS100COMNTOOLS environment variable.
EditorHelper: No Visual Studio 2010 installation found.
EditorHelper: Searching for Visual Studio 2012 installation using the VS110COMNTOOLS environment variable.
EditorHelper: No Visual Studio 2012 installation found.
EditorHelper: Searching for Visual Studio 2013 installation using the VS120COMNTOOLS environment variable.
EditorHelper: Found a Visual Studio 2013 installation here: c:/program files (x86)/microsoft visual studio 12.0/common7/
EditorHelper: Searching for Visual Studio 2015 installation using the VS140COMNTOOLS environment variable.
EditorHelper: Found a Visual Studio 2015 installation here: c:/program files (x86)/microsoft visual studio 14.0/common7/
EditorHelper: Your preferences indicate Visual Studio should be found here: c:/program files (x86)/microsoft visual studio 14.0/common7/ide/devenv.exe
EditorHelper: We found a matching Visual Studio 2015 installation here: c:/program files (x86)/microsoft visual studio 14.0/common7/
EditorHelper: Looking for a running Visual Studio session.
EditorHelper: We found for a running Visual Studio session with the solution open.
EditorHelper: Using the existing Visual Studio session.
EditorHelper: Getting operations API from the Visual Studio session.
EditorHelper: Waiting for the Visual Studio session to open the file: C:\Users\user\Desktop\RBLY-Android1\RBLY-Android\RBGY\Assets\Scripts\Adverts.cs.
Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 1.284538 seconds (Nothing changed)
Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 0.036662 seconds (Nothing changed)
Refresh: detecting if any assets need to be imported or removed ... Refresh: elapses 0.037274 seconds (Nothing changed)



WTF.png
Error.png

Veer Arjun Busani

unread,
Jan 14, 2016, 3:17:48 PM1/14/16
to Google Mobile Ads SDK Developers
Hi Dennis,

From your code snippet and the attached image, I would assume that you have an external script directly calling the GameOver() function like this - 
GoogleMobileAdsDemoScript adsScript = new GoogleMobileAdsDemoScript();
adsScript.gameOver();
Might I suggest you to create a singleton of the AdsScript class and try to load your Interstitial like this - 
  • In your GoogleMobileAdsDemoScript - 
private static GoogleMobileAdsDemoScript mInstance;
public static GoogleMobileAdsDemoScript manager {
    get {
        if (mInstance == null){
            GameObject go = new GameObject();
            mInstance = go.AddComponent<GoogleMobileAdsDemoScript>();
        }
        return mInstance;
    }
}

public void GameOver()
{
    // ensure that you have requested the Interstitial prior to this
    if (interstitial.IsLoaded())
    {
        interstitial.Show();
    }
}
And simply from your main class, do this - 
GoogleMobileAdsDemoScript.manager.GameOver();
If that does not solve your issue, do send us your minimal sample app for us to debug further. Do let us know if this solves your issue.

Thanks,
Veer Arjun Busani
Mobile Ads SDK Team

Dennis O

unread,
Jan 14, 2016, 3:26:06 PM1/14/16
to Google Mobile Ads SDK Developers
I'm not using the AdsDemoScript, this is my full script
using UnityEngine;
using GoogleMobileAds.Api;

public class Adverts : MonoBehaviour
{
    public static Adverts manager;
    private InterstitialAd interstitial;
    bool shown = false;

    void Awake()
    {
        manager = this;
    }

    public void RequestInterstitial()
    {
#if UNITY_ANDROID
        string adUnitId = "ca-app-pub-xxxxxxxxxxxxxxxxxxxxxxxxx";
#elif UNITY_IPHONE
       string adUnitId = "INSERT_IOS_INTERSTITIAL_AD_UNIT_ID_HERE";
#else
        string adUnitId = "unexpected_platform";
#endif
        interstitial = new InterstitialAd(adUnitId);
        AdRequest request = new AdRequest.Builder().Build();
        interstitial.LoadAd(request);
    }

    public void GameOver()
    {
        if (interstitial.IsLoaded())
        {
            interstitial.Show();
            shown = true;
        }
    }

    public void onAdClosed()
    {
        if (shown == true)
        {
            interstitial.Destroy();
            shown = false;
        }
    }
}


 

Dennis O

unread,
Jan 14, 2016, 3:37:59 PM1/14/16
to Google Mobile Ads SDK Developers
Also I don't have a Demoscript, do I need it?

Veer Arjun Busani

unread,
Jan 14, 2016, 3:40:22 PM1/14/16
to Google Mobile Ads SDK Developers
Hi Dennis,

It does look like you are following a Singleton pattern here for your Adverts.cs. The only thing I would want you to try is to change the Awake() method to the one I have pointed out earlier. 
private static Adverts mInstance;
public static Adverts manager {

    get {
        if (mInstance == null){
            GameObject go = new GameObject();

            mInstance = go.AddComponent<Adverts>();
        }
        return mInstance;
    }
}
Basically this would try to add the script as an GameObject and generate an instance of your Adverts script. Then simply make the call like - Adverts.manager.gameOver();

If this does not solve your issue, send us the script that is actually calling the Adverts class.

Dennis O

unread,
Jan 14, 2016, 3:52:28 PM1/14/16
to Google Mobile Ads SDK Developers

Yeah, I still get the null error.
This is the script calling it:
using UnityEngine;
using UnityEngine.UI;

public class UclickableButton : MonoBehaviour
{
    public static UclickableButton manager;
    public Button button;
    bool Ready;

    void Awake()
    {
        manager = this;
    }

    void Update()
    {
        Color color = GetComponent<Image>().color;

        if (color.a.Equals(0))
        {
            button.interactable = false;
            Ready = false;
        }

        if (color.a.Equals(1))
        {
            button.interactable = true;
            Ready = true;
        }

    }

    public void ReadyTrue()
    {
        while (Ready == true)
        {           
            Adverts.manager.GameOver();
            break;
        }
    } 
}

also you mean change the Advert script to this right?
using UnityEngine;
using GoogleMobileAds.Api;

public class Adverts : MonoBehaviour
{

    private InterstitialAd interstitial;
    bool shown = false;

Dennis O

unread,
Jan 14, 2016, 4:43:55 PM1/14/16
to Google Mobile Ads SDK Developers
Maybe this might help you diagnose the problem.
NullReferenceException: Object reference not set to an instance of an object
Adverts.GameOver () (at Assets/Scripts/Adverts.cs:31)
UclickableButton.ReadyTrue () (at Assets/Scripts/UclickableButton.cs:38)
ClickCounter.FixedUpdate () (at Assets/Scripts/ClickCounter.cs:27)

 

Veer Arjun Busani

unread,
Jan 14, 2016, 5:04:56 PM1/14/16
to google-adm...@googlegroups.com
Hi Dennis,

Two issues again here are -
  1. The Singleton design pattern again for your UclickableButton script. 
  2. Also seems like RequestInterstitial() was never called prior to showing Interstitial Ad.
I have made a sample project, in which there is a GameScript.cs, which acts as a component for the main camera. This script would be making those Interstitial requests via UIClickableButton to Adverts. Even I got the similar Null reference error and then I realized that the Interstitial Ad was never requested. 

Do let me know if you need anything else.

Project: Download Link

Dennis O

unread,
Jan 14, 2016, 6:43:11 PM1/14/16
to Google Mobile Ads SDK Developers
Everything worked fine for me before I don't understand why its doing that now, any ideas?

Dennis O

unread,
Jan 14, 2016, 7:29:18 PM1/14/16
to Google Mobile Ads SDK Developers
Also I'm still getting a null reference error after following what you did :/

Arjun Busani

unread,
Jan 14, 2016, 8:59:25 PM1/14/16
to Google Mobile Ads SDK Developers
Maybe send code from where you are actually making a request? Or send your entire application? That would help

Arjun Busani

unread,
Jan 14, 2016, 9:02:29 PM1/14/16
to Google Mobile Ads SDK Developers
Null reference could also mean that your Android SDK path is not setup properly. Maybe you could look into that.

Dennis O

unread,
Jan 14, 2016, 10:09:10 PM1/14/16
to Google Mobile Ads SDK Developers
I also tried removing all admob from my APK and then re imported it that didn't help.
All my scripts worked fine until I removed the Debug statements from DummyClient then hit save that. I then noticed it stated showing null for (interstitial.IsLoaded()):)
so I removed the DummyClient and imported a "fresh" one. The Null still persisted.
So I, like I said before I re imported admob and still the null persists. 
I have no clue what could be causing it. I even loaded a older version of my APK and then it started showing the null error in that when I didn't have an error in it before.

Arjun Busani

unread,
Jan 14, 2016, 10:14:49 PM1/14/16
to Google Mobile Ads SDK Developers
It looks like setup issue or how you are calling the Mobile Ads Script. Now it would be certainly be helpful if you could send your implementation app, as minimum as possible, just to replicate the issue.

Dennis O

unread,
Jan 14, 2016, 10:24:24 PM1/14/16
to Google Mobile Ads SDK Developers
What would I have to send over? The whole application or some specific stuff?.

Arjun Busani

unread,
Jan 14, 2016, 10:25:30 PM1/14/16
to Google Mobile Ads SDK Developers
The minimum possible that would replicate the issue

Dennis O

unread,
Jan 14, 2016, 10:30:03 PM1/14/16
to Google Mobile Ads SDK Developers
I'm going to first make a couple backup files then I'm going to reimport all my assets if that doesn't work I'll send over some files
Reply all
Reply to author
Forward
0 new messages