Thread: self is nil
View Single Post
12-25-14, 02:26 PM   #1
MrBerlin
A Murloc Raider
Join Date: Dec 2014
Posts: 4
self is nil

Hiho, here my code for the class:
Lua Code:
  1. BattleRecord = {};
  2. BattleRecord.__index = BattleRecord;
  3.  
  4. setmetatable(BattleRecord, {
  5.   __call = function (cls, ...)
  6.     return cls.new(...)
  7.   end,
  8. });
  9.  
  10. function BattleRecord.new(tamerId, petSpeciesIds, attackIds, triesTable)
  11.   local self = setmetatable({}, BattleRecord);
  12.   self.tamerId = tamerId;
  13.   self.petSpeciesIds= {{1,2,3},{4,5,6}};
  14.   --self.petSpeciesIds = petSpeciesIds;
  15.   self.rounds={};
  16.   self.attackIds = {};
  17.   self.tries = {0,0};
  18.   if (triesTable ~= nil) then
  19.     --self.tries = triesTable;
  20.   end
  21.   print("battleRecord init SElf:"..(tostring(self) or "nil"));
  22.   return self;
  23. end
  24. function BattleRecord:GetTriesTable()
  25.     return self.tries;
  26. end
  27. function BattleRecord:GetSuccessRate()
  28.    
  29.     print("self: "..tostring(self));
  30.     if (#self.tries == 0) then
  31.         return 0;
  32.     end
  33.     if (self.tries[2]>0) then
  34.         return Math.ceil(self.tries[1]/self.tries[2]);
  35.     end
  36.     return 0;
  37. end
  38. function BattleRecord:PrintRound(round)
  39.     local str = "Round: "..round..": ";
  40.     local trainer = self.trainers[1];
  41.     local roundTable = self.rounds[round];
  42.     if (#roundTable>0) then
  43.         local pet = trainer:GetPet(self.orders[round][1]);
  44.         local orderType = self.orders[round][2];
  45.         local orderValue = self.orders[round][3];
  46.         local orderTypes = {"", " uses ", "got switched to ", " used a trap ", " skipped the turn "};
  47.         str = str..pet:GetName();
  48.         str = str..orderTypes[orderType];
  49.         if (orderType==2) then
  50.             local abilityId, abilityName = C_PetBattles.GetAbilityInfoByID(pet:GetAbilityId(orderValue));
  51.             str = str..abilityName;
  52.         else
  53.             str = str..orderValue;
  54.         end
  55.     else
  56.         str = str.."no Action";
  57.     end
  58.  
  59.     print(str);
  60. end
  61. function BattleRecord:PrintAll()
  62.     print ("--- BATTLE RECORD ---");
  63.     print ("[TamerID: "..self.tamerId.."][NumRounds: "..#self.rounds.."][Successrate: "..self.GetSuccessRate().."%]");
  64.     print ("Teams:");
  65.     for t = 1, 2 do
  66.         local teamTable ={};
  67.         local str = " Team "..t..": [";
  68.         for p = 1, #self.petSpeciesIds[t] do
  69.             local id = self.petSpeciesIds[t][p];
  70.             local info = {C_PetJournal.GetPetInfoBySpeciesID(id)};
  71.             teamTable[p] = info[1];
  72.         end
  73.         str = str..table.concat(teamTable, ", ");
  74.         print(str);
  75.     end
  76.     for i=1, self.GetNumRounds() do
  77.         self.PrintRound(i);
  78.     end
  79. end
  80. function BattleRecord:GetRound(roundIndex)
  81.     return self.rounds[roundIndex];
  82. end
  83. function BattleRecord:GetTamerId()
  84.     return self.tamerId;
  85. end
  86. function BattleRecord:GetSpeciesId(ownerIndex, petIndex)
  87.     return self.petSpeciesIds[ownerIndex][petIndex];
  88. end
  89. function BattleRecord:AddRoundTable(roundTable)
  90.     self.rounds[#self.rounds+1] = roundTable;
  91. end
  92. function BattleRecord:GetNumRounds()
  93.     return #self.rounds;
  94. end
  95. function BattleRecord:AddRound(activePet, orderType, orderValue)
  96.     if (orderType~= nil) then
  97.         self:AddRoundTable({activePet, orderType, orderValue});
  98.     else
  99.         self:AddRoundTable({});
  100.     end
  101. end

when i run it and the BattleRecord:PrintAll() is called it gives me this error:
Code:
Date: 2014-12-25 21:13:24
ID: 1
Error occured in: Global
Count: 1
Message: ..\AddOns\CounterPet\class_BattleRecord.lua line 30:
   attempt to index local 'self' (a nil value)
Debug:
   CounterPet\class_BattleRecord.lua:30: GetSuccessRate()
   CounterPet\class_BattleRecord.lua:63: PrintAll()
   CounterPet\class_Battle.lua:63: End()
   CounterPet\CounterPet.lua:31:
      CounterPet\CounterPet.lua:24
Locals:
self = nil
(*temporary) = <function> defined @Interface\FrameXML\RestrictedInfrastructure.lua:116
(*temporary) = "self: nil"
(*temporary) = <function> defined =[C]:-1
(*temporary) = true
(*temporary) = "attempt to index local 'self' (a nil value)"
Why is self nil in the BattleRecord:GetSuccessRate? *veryconfused*
  Reply With Quote