webView:stop() makes an error ?

Hello

I have a google map inside a webview.

When the user clicks on a marker on the map, I intercept the url and make a webview:stop() to stop the browser action.
Then I have to go to another screen.
If I do make webview:stop(), I get an error within the xcode simulator :

Lua Runtime Error: lua\_pcall failed with status: 2, error message is: ?:0: attempt to index global 'webView' (a nil value)

I I don’t make a webview:stop(), I do not get the error but the native object stays in place.

here’s the code :

function scene:willEnterScene( event )  
 local group = self.view  
  
 local hauteurMax = display.viewableContentHeight - 130  
 local largeurMax = 312  
 local top = 20  
  
  
 local function webListener(event)  
  
 if event.type == "other" then   
 local url = event.url  
 local r  
 r, url = utils.checkForUrlEmptyness(url)  
 if r then  
 local monNomVille  
 local monIdVille  
 local monPaysVille  
  
 monNomVille, monIdVille, monPaysVille = dataBase.recupVilleNom(url)  
 local options = {  
 effect = "slideLeft",  
 time = 500,  
 params = {   
 nomVille = monNomVille,  
 idVille = tostring(monIdVille),  
 paysVille = monPaysVille  
 }  
 }  
 webView:stop()  
 storyboard.gotoScene( "cities2", options )  
  
 end  
 end   
 local webView = native.newWebView(2, top, largeurMax, hauteurMax)  
 webView:request("gmap\_world.html", system.CachesDirectory )  
 webView:addEventListener("urlRequest", webListener)  
 group:insert(webView)  
end  
  
function scene:exitScene( event )  
 local group = self.view  
 group:removeSelf()   
end  

I do not find a work around. Maybe I am doing something wrong ? [import]uid: 5578 topic_id: 26713 reply_id: 326713[/import]

The variable ‘webView’ you use to call webView:stop() on line 30 has not been declared/instantiated yet. You declare/assign ‘local webView’ on line 35.

Since the webView you use on line 30 has not been declared earlier, you are accessing a global variable called ‘webView’. This variable is nil by default. Hence, you are calling
nil:stop()
which produces the error you see.
[import]uid: 7563 topic_id: 26713 reply_id: 108452[/import]

Thanks a lot ! I thought the listener would be “instantiate” after the creation of the webView, but it seems the code is read and executed from the top to the bottom ;-)) [import]uid: 5578 topic_id: 26713 reply_id: 108469[/import]

It does instantiate in the order you thought (this is run-time). The problem is that the variable declarations are from top to bottom (this is compile-time). The ‘local webView’ is a different variable than the global ‘webView’ (nil).

Declare ‘local webView’ at the top of your function scene:willEnterScene() (line 2) and remove the ‘local’ keyword on line 35.
[import]uid: 7563 topic_id: 26713 reply_id: 108512[/import]

That’s I have done. Thanks again.
Variables should be declared at the top of the script, I will remember.

:wink:

[import]uid: 5578 topic_id: 26713 reply_id: 108532[/import]