Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Null pointer exception when use Advertisement.Initialize unity

Discussion in 'Unity Ads & User Acquisition' started by BossBarforn, Jul 29, 2020.

  1. BossBarforn

    BossBarforn

    Joined:
    Nov 29, 2018
    Posts:
    2
    Iam using the latest unity version is : 2020.1.0 and Unity Monetization 3.4.6 but when i try to use ads in the game ( android platform, both jdk and sdk are use from unity recomend) i get this kind of error. I even try to use the package manager 3.4.7 but it is not work nether .
    Code (CSharp):
    1.  private void Start()
    2.     {
    3.      
    4.         Advertisement.Initialize(gameId);
    5.  
    6.     }
    7.    
    NullReferenceException: Object reference not set to an instance of an object
    System.Net.AutoWebProxyScriptEngine.InitializeRegistryGlobalProxy () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.AutoWebProxyScriptEngine.GetWebProxyData () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebProxy.UnsafeUpdateFromRegistry () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebProxy..ctor (System.Boolean enableAutoproxy) (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebProxy.CreateDefaultProxy () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.Configuration.DefaultProxySectionInternal.GetSystemWebProxy () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.Configuration.DefaultProxySectionInternal.GetDefaultProxy_UsingOldMonoCode () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.Configuration.DefaultProxySectionInternal.GetSection () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebRequest.get_InternalDefaultWebProxy () (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.HttpWebRequest..ctor (System.Uri uri) (at <525dc68fbe6640f483d9939a51075a29>:0)
    (wrapper remoting-invoke-with-check) System.Net.HttpWebRequest..ctor(System.Uri)
    System.Net.HttpRequestCreator.Create (System.Uri uri) (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebRequest.Create (System.Uri requestUri, System.Boolean useUriBase) (at <525dc68fbe6640f483d9939a51075a29>:0)
    System.Net.WebRequest.Create (System.String requestUriString) (at <525dc68fbe6640f483d9939a51075a29>:0)
    UnityEngine.Advertisements.Platform.Editor.EditorPlatform.Initialize (System.String gameId, System.Boolean testMode, System.Boolean enablePerPlacementLoad) (at Library/PackageCache/com.unity.ads@3.4.5/Runtime/Advertisement/Platform/Editor/EditorPlatform.cs:85)
    UnityEngine.Advertisements.Platform.Platform.Initialize (System.String gameId, System.Boolean testMode, System.Boolean enablePerPlacementLoad) (at Library/PackageCache/com.unity.ads@3.4.5/Runtime/Advertisement/Platform/Platform.cs:64)
    UnityEngine.Advertisements.Advertisement.Initialize (System.String gameId) (at Library/PackageCache/com.unity.ads@3.4.5/Runtime/Advertisement/Advertisement.cs:57)
    AdManager.Start () (at Assets/Scripts/Shop/AdManager.cs:24)

     
  2. sbankhead

    sbankhead

    Unity Technologies

    Joined:
    Jul 27, 2014
    Posts:
    97
    Could you shed a little more light on your environment and whats going on? This error looks to be occurring when running in play mode in the editor. Can you verify for me you have internet access and your not running anything locally that would be blocking our configuration request, such as Charles Proxy? Even if you were blocking the request an exception is not right and we would like to address this issue for anyone else that might be seeing it.
     
  3. ShinyOpal

    ShinyOpal

    Joined:
    Dec 17, 2022
    Posts:
    32
    Why not invoke the Initialize with the 3 parameters

    Code (CSharp):
    1. Advertisement.Initialize(_gameId, _testMode, this);
    Your code ends up in this method

    Code (CSharp):
    1. public static void Initialize (string gameId, bool testMode, IUnityAdsInitializationListener initializationListener)
    2.     {
    3.         if (initializationListener == null) {
    4.             Debug.LogError ((object)"initializationListener is null, you will not receive any callbacks");
    5.         }
    6.         if (!testMode && UnityAdsSettings.get_testMode ()) {
    7.             Debug.Log ((object)"Unity Ads is initializing in test mode since test mode is enabled in Service Window.");
    8.         }
    9.         s_Platform.Initialize (gameId, UnityAdsSettings.get_testMode () || testMode, new UnityAdsInitializationListenerMainDispatch (initializationListener, s_Platform.UnityLifecycleManager));
    10.     }
    Based on your log, you are not in the testMode nor do you have the test mode set in the UnityEditor. If very likely executes

    Code (CSharp):
    1. s_Platform.Initialize (gameId, UnityAdsSettings.get_testMode () || testMode, new UnityAdsInitializationListenerMainDispatch (initializationListener, s_Platform.UnityLifecycleManager));
    The
    received the initializationListener as null. Maybe that is where it breaks and it throws NullReferenceException.