Why does "Error: No object to invoke" occur here? Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
User avatar
hoppfrosch
Posts: 374
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Why does "Error: No object to invoke" occur here?

13 May 2019, 01:35

What does the error message "Error: No object to invoke." mean exactly? When does it occur?

I've got the following code sequence (excerpt), where the error "Error: No object to invoke." occurs - but I cannot figure out, why this happens...

Using latest AHK2 (a100 to a103)

Code: Select all

settings := A_ScriptDir "/settings.json"
str := FileRead(settings)
obj := JSON.Load(str )   ; <-- works correctly 
OutputDebug("Settings: " obj2str(obj))

gh := new github(obj.github.name, obj.github.email, obj.github.token)
x := gh.users.getAuthenticatedUser()
obj2 := JSON.Load(x)  ; <--- !!! ERROR: No object to invoke occurs
OutputDebug(x)

ExitApp

class github {
	class access {
		get() {
			json :=this.Send("GET",this._url)
			return json
		}
		send(verb,url,data:="",content_type:="application/json"){
			this.http.Open(verb,url)
			if !(data == "") {
				this.http.SetRequestHeader("Content-Typ",content_type)
			}
			this.http.send(data)
			return this.http.ResponseText
		}
	}
	class users {
		getAuthenticatedUser() {
			this.access.url := "/user"
			jsonX := this.access.get()
			OutputDebug("< ret: " jsonX)   ; <--- jsonX has the correct content
			return jsonX
		}
	}
}

Running this, I got the following error.

Code: Select all

---------------------------
Demo_Repos.ahk
---------------------------
Error:  No object to invoke.

	Line#
	018: OutputDebug("DBGVIEWCLEAR")
	022: settings := A_ScriptDir "/settings.json"
	023: str := FileRead(settings)
	024: obj := JSON.Load(str )
	025: OutputDebug("Settings: " obj2str(obj))
	027: gh := new github(obj.github.name, obj.github.email, obj.github.token)
	028: x := gh.users.getAuthenticatedUser()
--->	029: obj2 := JSON.Load(x)
	030: OutputDebug(x)
	031: ExitApp()
	032: Exit
	033: Exit
	033: Exit

The current thread will exit.
---------------------------
OK   
---------------------------
Why is jsonX not returned correctly from gh.users.getAuthenticatedUser()?
Last edited by hoppfrosch on 14 May 2019, 04:19, edited 1 time in total.
User avatar
aseiot
Posts: 33
Joined: 05 Mar 2017, 04:25

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?  Topic is solved

13 May 2019, 02:49

I think the problem is you reset the var "json"

Code: Select all

class github {
	class access {
		get() {
			local json :=this.Send("GET",this._url)	; here, try to define it as a local var
			return json
		}
		send(verb,url,data:="",content_type:="application/json"){
			this.http.Open(verb,url)
			if !(data == "") {
				this.http.SetRequestHeader("Content-Typ",content_type)
			}
			this.http.send(data)
			return this.http.ResponseText
		}
	}
	class users {
		getAuthenticatedUser() {
			this.access.url := "/user"
			jsonX := this.access.get()
			OutputDebug("< ret: " jsonX) 
			return jsonX
		}
	}
}
btw, not tested!
swagfag
Posts: 2981
Joined: 11 Jan 2017, 17:59

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

13 May 2019, 03:28

i dont understand how ur class is supposed to work(and how it has allegedly ever worked to begin with)
what u got going on basically is this, unless ure rigging them up in some other way that u arent showing:

Code: Select all

class Github
{
	class Access
	{
		getAccess() {
			MsgBox 'called getAccess()'
		}
	}

	class Users
	{
		getUsers() {
			MsgBox 'called getUsers()`ncalling next this.Access.getAccess() from Users'
			this.Access.getAccess() ; <<<<<<<<< no object to invoke
			; 'this' refering to 'class Users' which itself doesnt have 'Access', 
			; which in turn doesnt have a 'getAccess()'
		}
	}
}

gh := new Github()
gh.Users.getUsers()
lexikos
Posts: 6652
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

13 May 2019, 16:09

The error message means that you tried to invoke a string or number, and the operation was not handled by the default base object.

That is, obj2 := JSON.Load(x) attempts to invoke JSON, which is not an object. There is no object.

If I add default methods or properties to strings and/or numbers, I'll probably change the message to "Unknown property" or "Unknown method". Eventually error reporting will be enhanced to include the type of the object/value being invoked, and a stack trace. The error dialog part of Object.ahk adds some detail like this, but some of it relies on details being added to the thrown exceptions (and Object.ahk throws custom exceptions).
User avatar
hoppfrosch
Posts: 374
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

13 May 2019, 23:48

lexikos wrote:
13 May 2019, 16:09
The error message means that you tried to invoke a string or number, and the operation was not handled by the default base object.

That is, obj2 := JSON.Load(x) attempts to invoke JSON, which is not an object. There is no object.
:think: Just to understand: Why is the first call obj := JSON.Load(x) a few lines above working - and the second call obj2 := JSON.Load(x) failing?

If I do something like this, there is no error:

Code: Select all

settings := A_ScriptDir "/settings.json"
str := FileRead(settings)
obj := JSON.Load(str )   ; <-- works correctly 
OutputDebug("Settings: " obj2str(obj))

settings2 := A_ScriptDir "/settings.json"
str2 := FileRead(settings)
obj2 := JSON.Load(str2)   ; <-- works correctly 
OutputDebug("Settings2: " obj2str(obj2))
BTW: I'm using Cocobelgia's JSON-Library: https://www.autohotkey.com/boards/viewtopic.php?f=76&t=62658&hilit=JSON#p267828
swagfag
Posts: 2981
Joined: 11 Jan 2017, 17:59

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

14 May 2019, 00:29

did u consider what @aseiot wrote?
rerun ur scripts with #warn classoverwrite
User avatar
hoppfrosch
Posts: 374
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

14 May 2019, 01:32

@swagfag , @aseiot : Thanks

That was it ... silly me :headwall: :crazy: : I used a variable named json at several places - and used a class named "JSON" as well

#warn classoverwrite showed me this... Renaming the variables solved the problem :bravo:
lexikos
Posts: 6652
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

14 May 2019, 02:38

I've moved the topic to Ask for Help. You had a problem with a script, not related to v2 Development. Your error is not even specific to v2, only the obvious symptom (error message).
User avatar
hoppfrosch
Posts: 374
Joined: 07 Oct 2013, 04:05
GitHub: hoppfrosch
Location: Rhine-Maine-Area, Hesse, Germany
Contact:

Re: [a100 ... a103] Why does "Error: No object to invoke" occur here?

14 May 2019, 04:18

:thumbup:

Return to “Ask For Help”

Who is online

Users browsing this forum: just me and 192 guests