AllDiscussions:13: bad argument #1 to 'insert' (table expected, got nil)
stack traceback:
[C]: in function 'table.insert'
AllDiscussions:13: in method 'addDiscussion'
CreateTopicPage:120: in method 'post'
CreateTopicPage:61: in method 'touched'
Main:56: in function 'touched'
I don’t understand why he isn’t recognising my table? The table is a self an is located in init.
AllDiscussions = class()
-- Nothing more the General
function AllDiscussions:init()
self.y = 0
self.touch = false
self.dynamic = {DiscussionType("Test Topic", "General", "TokOut", 280, [[This topic includes a line<break>break because it is too long for<break>the positions. Ill try to fix<break>all bugs, because with bugs there<break>is no fun]])}
self.p = 0
end
function AllDiscussions:addDiscussion(name, category, own, data)
local p = self.p
table.insert(self.dynamic, DiscussionType(name, category, own, p, data))
end
function AllDiscussions:draw()
self.p = 280 - #self.dynamic * 110
-- Discussions/Draw them
self.creatediscussion = CreateTopic(self.y + 500)
self.creatediscussion:draw()
self.settings = SettingBox(self.y + 390)
self.settings:draw()
for _,d in pairs(self.dynamic) do d:draw() end
end
function AllDiscussions:touched(t)
-- Action List
self.creatediscussion:touched(t)
self.settings:touched(t)
-- Main Action
if t.state == ENDED then
self.touch = false
local currentY = 0
else
if t.y < 650 then
self.touch = true
local currentY = t.y
translate(0, currentY)
if currentY > self.y then
self.y = self.y + t.deltaY/2
else
self.y = self.y - t.deltaY/2
end
translate(0, -currentY)
for _,d in pairs(self.dynamic) do
d:touched(t)
end
end
end
end
-- This function is nil
function AllDiscussions:keyboard(key)end
can you post the code that creates the instance of AllDiscussions, (ie something = AllDiscussions(args) and the code that calls the addDiscussion method in createTopicPage please.
@yojimbo2000, this is the code where I add a new discussion:
function CreateTopicPage:post()
if string.len(self.text.message)==0 or string.len(self.text.title)==0 or self.category==nil then
print("Failed: Title, body or nil category")
self.selected = 1
else
local t = title
local b = body
local c = cat
currentPage = AllDiscussions()
AllDiscussions:addDiscussion(t, c, "Non User", b)
end
end
Wait, here I had this code. That was from another project:
function CreateTopicPage:post()
if string.len(self.text.message)==0 or string.len(self.text.title)==0 or self.category==nil then
print("Failed: Title, body or nil category")
self.selected = 1
else
local t = self.text.title
local b = self.text.message
local c = self.category
currentPage = AllDiscussions()
AllDiscussions:addDiscussion(t, c, "Non User", b)
end
end
--[[
AllDiscussions:13: bad argument #1 to 'insert' (table expected, got nil)
stack traceback:
[C]: in function 'table.insert'
AllDiscussions:13: in method 'addDiscussion'
CreateTopicPage:120: in method 'post'
CreateTopicPage:61: in method 'touched'
Main:56: in function 'touched'
]]
You need to call a method on an instance of a class, so it should be currentPage:addDiscussion(), not AllDiscussions:addDiscussion() otherwise, the method won’t be able to access the correct self variable. That’s why instead of seeing a table, addDiscussion just found nil.You need to make sure you do this each time you call a class method.