Thread Tools Display Modes
06/22/14, 09:57 PM   #1
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
Attempt to index a nil value confusion

Hi all.

I'm trying to access an variable with a value from my saved variables, but ESO is complaining that the variable is nil.

This is the line in question:

Lua Code:
  1. if(Imperialization.savedVariables.ConvertOnEquip == true) then

This is executed by a function fired by EVENT_INVENTORY_SINGLE_SLOT_UPDATE.

If this line is not present, references just a few lines down in the same function to other items in my saved variables are picked up just fine. The error also only comes up at login, but not after a ui reload.

I've tried deleting the saved variables .lua file and starting from scratch, but still this issue persists.

Any help would be greatly appreciated!
  Reply With Quote
06/22/14, 11:55 PM   #2
Sasky
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 231
Is the variable for your SavedVars properly initialized before this?

The error is happening because either Imperialization is nil or Imperialization.savedVariables is nil.

(Also check for typo in those two. Always happens when you least expect it...)

Last edited by Sasky : 06/22/14 at 11:57 PM.
  Reply With Quote
06/23/14, 12:26 AM   #3
farangkao
 
farangkao's Avatar
AddOn Author - Click to view addons
Join Date: May 2014
Posts: 59
And remember you can safeguard your code

if Imperialization.savedVariables then
if Imperialization.savedVariables.ConvertOnEquip then
-- do your stuff
end

else
d("Error No Saved Variables here")
end




When you do a test like "if <variable> then" ,the code will only run if the variable is not nil or not false (nil = means not initialized)

Last edited by farangkao : 06/23/14 at 12:39 AM.
  Reply With Quote
06/23/14, 03:18 AM   #4
Garkin
 
Garkin's Avatar
AddOn Author - Click to view addons
Join Date: Mar 2014
Posts: 832
The easiest way to fix this issue is to register event after you have saved variables available - i.e. move event registration to the Initialize function:
Lua Code:
  1. function Imperialization:Initialize()
  2.     Imperialization.savedVariables = ZO_SavedVars:New("ImperializationVariables", Imperialization.version, nil, Imperialization.Default)
  3.     LAM2:RegisterAddonPanel("ImperializationSettings", panelData)
  4.     LAM2:RegisterOptionControls("ImperializationSettings", optionsData)
  5.     EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, Imperialization.OnInventorySlotUpdate)
  6.     EVENT_MANAGER:UnregisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED)
  7. end
  8.  
  9. EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED, Imperialization.OnAddOnLoaded)

By the way I think it's not a good idea to use addon version as a version of your saved variables. It will reset everything to the defaults with each new addon version.

EDIT:
Another suggestion - Conditions you use looks rather complicated. If you use itemSyle as a key in your saved variables, you can use much easier condition:

lua Code:
  1. local itemStyle = select(7, GetItemInfo(bagID, slotID))
  2. if Imperialization.savedVariables[itemStyle] == true then
  3.     if(Imperialization.savedVariables.DisplayResults) then
  4.         d(zo_strformat("<<t:1>> converted from the <<2>> style!", GetItemLink(bagID, slotID, LINK_STYLE_BRACKETS), GetString("SI_ITEMSTYLE", itemStyle)))
  5.     end
  6. end

Last edited by Garkin : 06/23/14 at 03:29 AM.
  Reply With Quote
06/23/14, 09:46 AM   #5
Randactyl
AddOn Author - Click to view addons
Join Date: Apr 2014
Posts: 251
Originally Posted by Garkin View Post
The easiest way to fix this issue is to register event after you have saved variables available - i.e. move event registration to the Initialize function:
Lua Code:
  1. function Imperialization:Initialize()
  2.     Imperialization.savedVariables = ZO_SavedVars:New("ImperializationVariables", Imperialization.version, nil, Imperialization.Default)
  3.     LAM2:RegisterAddonPanel("ImperializationSettings", panelData)
  4.     LAM2:RegisterOptionControls("ImperializationSettings", optionsData)
  5.     EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, Imperialization.OnInventorySlotUpdate)
  6.     EVENT_MANAGER:UnregisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED)
  7. end
  8.  
  9. EVENT_MANAGER:RegisterForEvent(Imperialization.name, EVENT_ADD_ON_LOADED, Imperialization.OnAddOnLoaded)

By the way I think it's not a good idea to use addon version as a version of your saved variables. It will reset everything to the defaults with each new addon version.

EDIT:
Another suggestion - Conditions you use looks rather complicated. If you use itemSyle as a key in your saved variables, you can use much easier condition:

lua Code:
  1. local itemStyle = select(7, GetItemInfo(bagID, slotID))
  2. if Imperialization.savedVariables[itemStyle] == true then
  3.     if(Imperialization.savedVariables.DisplayResults) then
  4.         d(zo_strformat("<<t:1>> converted from the <<2>> style!", GetItemLink(bagID, slotID, LINK_STYLE_BRACKETS), GetString("SI_ITEMSTYLE", itemStyle)))
  5.     end
  6. end
Wow, Garkin you really went above and beyond! Thank you, and everyone else, so much for the help!
Now that it has been pointed out the mistake seems silly. And yes, my conditions quickly got out of hand
Now seems like the best time to reign all of this stuff back in.
  Reply With Quote

ESOUI » Developer Discussions » Lua/XML Help » Attempt to index a nil value confusion


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