Thread Tools Display Modes
12-21-09, 05:14 PM   #1
thejoester
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jun 2006
Posts: 10
Error 'attempt to concatenate global'

ok I am getting this error in my addon

Date: 2009-12-21 16:09:03
ID: 1
Error occured in: Global
Count: 1
Message: ...terface\AddOns\JoesEclipseAlert\JoesEclipseAlert.lua line 84:
attempt to concatenate global 'SID' (a nil value)
Debug:
[C]: ?
...terface\AddOns\JoesEclipseAlert\JoesEclipseAlert.lua:84: JoesEclipseAlert_OnEvent()
[string "*:OnEvent"]:1:
[string "*:OnEvent"]:1
The line of code it is erroring on is

Code:
debugLog = "DEBUG: Spell ID:" .. SID;
I have delared this in my OnLoad() function and assigned it a value of one, but I still get this error.

any help?
  Reply With Quote
12-21-09, 05:17 PM   #2
Xrystal
nUI Maintainer
 
Xrystal's Avatar
Premium Member
AddOn Author - Click to view addons
Join Date: Feb 2006
Posts: 5,892
Prior to that line add the following :

print("DEBUG: Spell ID:",SID);

Then see what it shows in your chat window. Maybe it is getting to that line prior to the OnLoad call. You can test this by setting SID at the top of your lua file it is used in and set it to a value there.
__________________
  Reply With Quote
12-21-09, 06:37 PM   #3
Akryn
A Firelord
AddOn Author - Click to view addons
Join Date: Mar 2008
Posts: 479
Don't create a global named SID. Put it in a namespace or make it local. Calling a global that is just asking for some other addon to overwrite it.

Also, what Xrystal said.
  Reply With Quote
12-22-09, 12:00 AM   #4
thejoester
A Deviate Faerie Dragon
AddOn Author - Click to view addons
Join Date: Jun 2006
Posts: 10
Originally Posted by Xrystal View Post
Prior to that line add the following :

print("DEBUG: Spell ID:",SID);

Then see what it shows in your chat window. Maybe it is getting to that line prior to the OnLoad call. You can test this by setting SID at the top of your lua file it is used in and set it to a value there.

OK i found the issue, I was assigning its value from UnitBuff() and passing it the spell id, however it doesn't always have a value for spell id.

So I created a function to check the value and if it was nil then I assigned it "none" and that fixed it.

Also, I only made it a global variable because I was trying to get rid of the error. I have made it local again.
  Reply With Quote
12-22-09, 04:27 AM   #5
Taroven
A Cyclonian
AddOn Author - Click to view addons
Join Date: Dec 2006
Posts: 49
Originally Posted by thejoester View Post
So I created a function to check the value and if it was nil then I assigned it "none" and that fixed it.
Better trick might be to use a quick inline expression. You can also do the same to validate the spellID.
Code:
debugLog = "DEBUG: Spell ID:" .. SID or "none"
...which basically reads like "if SID then spellID else none". For extra conditions:
Code:
foo = foo and bar or lol and boo or bar == boo and nil
--[[ if foo then bar
elseif lol then boo
elseif bar == boo then nil
]]

-- In practical application, ripped from EventHorizon's guts...
for i in pairs(frame) do
	local gr = frame[i].glyphrefresh or nil
	local gs = frame[i].glyphstacks or nil
	if gr and (gr[3] == spellname) then
		if gs[destguid] then
			gs[destguid] = gs[destguid] - 1
			frame[i].stacks:SetText(gs[destguid] > 0 and gs[destguid] or nil)
		end
		--debug("SUCCESS! "..gr[3].." has triggered "..frame[i].auraname)
	end
end
Edit/note: If working with tables, an entry that exists but is set to nil acts differently than a variable. If you just want to check if the table index is there and don't care about the contents, cool, but if you're doing anything that'll error on nil you need to specify what you want.
Code:
local foo = nil
print(foo or "LOL")
-- Result: "LOL"

local foo = {}
foo.bar = nil
print(foo.bar or "LOL")
-- Result: nil

-- Now to specify what we're looking for.
print(foo.bar ~= nil and foo.bar or "LOL")
-- Result: "LOL"

foo.bar = "HAITHAR"
print(foo.bar ~= nil and foo.bar or "LOL")
-- Result: "HAITHAR"
Hopefully that's all somewhat relevant and makes sense. Not something you're gonna use every line of code, but it does help a lot, especially when working with variables that have potential to nil out on you.
__________________
Former author of EventHorizon Continued and Other Releases.

Last edited by Taroven : 12-22-09 at 04:44 AM. Reason: Added stuff.
  Reply With Quote
12-28-09, 01:18 AM   #6
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by Taroven View Post
Code:
local foo = {}
foo.bar = nil
print(foo.bar or "LOL")
-- Result: nil
That's odd, because the expression nil acts as false, so this should return "LOL" as well regardless of whether or not nil was manually assigned to the variable or table entry.

Edit: Just to make sure, I ran this through Lua and it works as I had described.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 12-28-09 at 05:58 AM. Reason: 1:Confirmed 2:Code block used for better distinction in quote
  Reply With Quote
12-28-09, 04:57 AM   #7
nightcracker
A Molten Giant
 
nightcracker's Avatar
AddOn Author - Click to view addons
Join Date: Sep 2009
Posts: 716
Originally Posted by SDPhantom View Post
That's odd, because the expression nil acts as false, so this should return "LOL" as well regardless of whether or not nil was manually assigned to the variable or table entry.

Edit: Just to make sure, I ran this through Lua and it works as I had described.
Note that if you are trying to get a value from a table while the variable is no table at all, it will always fail, even when you have an "or" set. Then you should you this:

Code:
print(type(foo)=="table" and foo.bar or "LOL")
__________________
Three things are certain,
Death, taxes and site not found,
You, victim of one.
  Reply With Quote
12-28-09, 05:50 AM   #8
SDPhantom
A Pyroguard Emberseer
 
SDPhantom's Avatar
AddOn Author - Click to view addons
Join Date: Jul 2006
Posts: 2,313
Originally Posted by nightcracker View Post
Note that if you are trying to get a value from a table while the variable is no table at all, it will always fail, even when you have an "or" set.
While attempting to index a nil variable will result in Lua throwing an error and terminating execution, this is not the purpose of the above poster's explanation. My post was a correction on Lua's behavior in the specific operation performed.
__________________
WoWInterface AddOns
"All I want is a pretty girl, a decent meal, and the right to shoot lightning at fools."
-Anders (Dragon Age: Origins - Awakening)

Last edited by SDPhantom : 12-28-09 at 05:57 AM.
  Reply With Quote

WoWInterface » Developer Discussions » Lua/XML Help » Error 'attempt to concatenate global'

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off