New admob plugin. I am getting a NullPointerException. Why?

Hi,

Why am I getting a java.lang.NullPointerException when I try the following code in main.lua? 

Here is my main.lua. Error occurs at the admob.load line.

local admob = require( "plugin.admob" ) local function admobAdListener( event )     if ( event.phase == "init" ) then  -- Successful initialization         print( event.provider )     end end   admob.init( admobAdListener, { appId="ca-app-pub-\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*~\*\*\*\*\*\*\*\*\*\*\*\*\*" } )  --Notice the "~" This is correct right? local function onSystemEvent( event ) if ( event.type == "applicationStart" ) then     admob.load( "interstitial", { adUnitId="ca-app-pub-\*\*\*\*\*\*\*\*\*\*\*\*\*\*/\*\*\*\*\*\*\*\*\*\*\*\*" } ) end end

Here is the error when deployed on a device. Device is old (API 16) maybe thats the problem but I have successfully managed to load and show ads but in another scene. Why am I getting this error at main.lua?

I/Corona ( 5122): ERROR: Runtime error I/Corona ( 5122): main.lua:707: java.lang.NullPointerException I/Corona ( 5122): Java Stack Trace: I/Corona ( 5122): plugin.admob.LuaLoader$Load.invoke(LuaLoader.java:670) I/Corona ( 5122): com.ansca.corona.JavaToNativeShim.nativeRender(Native Method) I/Corona ( 5122): com.ansca.corona.JavaToNativeShim.render(JavaToNativeShim.java:182) I/Corona ( 5122): com.ansca.corona.Controller.updateRuntimeState(Controller.java:347) I/Corona ( 5122): com.ansca.corona.graphics.opengl.CoronaGLSurfaceView$CoronaRenderer.onDrawFrame(CoronaGLSurfaceView.java:421) I/Corona ( 5122): com.ansca.corona.graphics.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1623) I/Corona ( 5122): com.ansca.corona.graphics.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1378)

The plugin must initialize properly before calling other AdMob API functions. Move admob.load() to the admobAdListener function after checking for the “init” phase.

This solved it. 

One other issue I am having is I am loading the ads in main.lua as shown and then I go to another scene. While in main.lua I confirm that the ad is loaded with print( admob.isLoaded( “banner” ) ). It returns true after a couple of seconds.

Now when I go to another scene 

local composer = require( "composer" ) local admob = require( "plugin.admob" ) function scene:show( event ) local group = self.view admob.show( "banner", { y="bottom" } ) end

the banner that is confirmed to be loaded in main.lua is not displayed and I get the error “WARNING: admob.load(adType, options), Banner not loaded”

Why is this and how can I fix it? Why am I losing the banner I loaded in the previous scene. Do I need to load() and init() in every scene?

That’s a bit strange. It shouldn’t lose the banner between scene changes. I’ll investigate once I get back on Tuesday after the holidays. In the mean time, there is no harm in issuing a load() after you change scene, however init() should only be called once.

Figured out whats happening.

I load a banner in main.lua and then I attempt to load a banner again at scene:show() when I change to another scene. So I am losing the previously load banner when I attempt load a new one.

This should not happen. If I am attempting to load an already loaded ad unit my attempt should be ignored and my previously load ad should not be discarded. It wasn’t being discarded in admob v2. 

In that case you should move isLoaded() to your new scene before you try to load a banner again.

Thats what I did. Thank you.

The plugin must initialize properly before calling other AdMob API functions. Move admob.load() to the admobAdListener function after checking for the “init” phase.

This solved it. 

One other issue I am having is I am loading the ads in main.lua as shown and then I go to another scene. While in main.lua I confirm that the ad is loaded with print( admob.isLoaded( “banner” ) ). It returns true after a couple of seconds.

Now when I go to another scene 

local composer = require( "composer" ) local admob = require( "plugin.admob" ) function scene:show( event ) local group = self.view admob.show( "banner", { y="bottom" } ) end

the banner that is confirmed to be loaded in main.lua is not displayed and I get the error “WARNING: admob.load(adType, options), Banner not loaded”

Why is this and how can I fix it? Why am I losing the banner I loaded in the previous scene. Do I need to load() and init() in every scene?

That’s a bit strange. It shouldn’t lose the banner between scene changes. I’ll investigate once I get back on Tuesday after the holidays. In the mean time, there is no harm in issuing a load() after you change scene, however init() should only be called once.

Figured out whats happening.

I load a banner in main.lua and then I attempt to load a banner again at scene:show() when I change to another scene. So I am losing the previously load banner when I attempt load a new one.

This should not happen. If I am attempting to load an already loaded ad unit my attempt should be ignored and my previously load ad should not be discarded. It wasn’t being discarded in admob v2. 

In that case you should move isLoaded() to your new scene before you try to load a banner again.

Thats what I did. Thank you.