List issue when i change the items table

To get it out of the way, I am a noob to lua and corona.

I have a question that’s probably simple but i have not been able to find or fix myself. I am using the list demo and wanted to change the list items. Here is what I did.

*************************************
– Initialize a list of data items
items = {}
items[1] = “test1”
items[2] = “here2”
items[3] = “here3”
items[4] = “here4”
items[5] = “here5”
items[6] = “here6”
items[7] = “here7”
items[8] = “here8”

–for numItems = 1,1000 do
– items[numItems] = “test”…numItems
–end
****************************************

The list shows but I get the error below?

*****************************************
Runtime error
bad argument #1 to ‘_newText’ (string expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘_newText’
?: in function ‘newText’
/Users/owner1/Downloads/list_demo 2/main.lua:204: in function ‘renderer’
/Users/owner1/Downloads/list_demo 2/main.lua:146: in function ‘createRenderers’
/Users/owner1/Downloads/list_demo 2/main.lua:17: in function ‘init’
/Users/owner1/Downloads/list_demo 2/class.lua:27: in function ‘List’
/Users/owner1/Downloads/list_demo 2/main.lua:252: in main chunk
Runtime error: bad argument #1 to ‘_newText’ (string expected, got nil)
stack traceback:
[C]: ?
[C]: in function ‘_newText’
?: in function ‘newText’
/Users/owner1/Downloads/list_demo 2/main.lua:204: in function ‘renderer’
/Users/owner1/Downloads/list_demo 2/main.lua:146: in function ‘createRenderers’
/Users/owner1/Downloads/list_demo 2/main.lua:17: in function ‘init’
/Users/owner1/Downloads/list_demo 2/class.lua:27: in function ‘List’
/Users/owner1/Downloads/list_demo 2/main.lua:252: in main chunk
*********************************************** [import]uid: 8000 topic_id: 1691 reply_id: 301691[/import]

I did notice that if i put in a “if data ~= nil then” around newText in the List:render function the error goes away. i am not sure if this is really a fix or just a fix to something i am doing wrong? The code is below.
[lua]function List:renderer(displayObject, data, selected)
if displayObject == nil then
local img

displayObject = display.newGroup()

if selected == true then
img = display.newImage(self.skin.selectedBackground)
img.selected = true
else
img = display.newImage(self.skin.background)
img.selected = false
end
print (data)
if data ~= nil then
local text = display.newText(data, 0, 0, native.systemFontBold, 12);
text:setTextColor(0,0,0)
text.y = img.y
text.x = text.x + 5

displayObject:insert(img)
displayObject:insert(text)

displayObject.img = img
displayObject.text = text
end
else
displayObject.text.text = data
displayObject.text.x = displayObject.text.width/2 + 5

if(displayObject.img.selected ~= selected) then
displayObject:remove(displayObject.img)

if selected == true then
displayObject.img = display.newImage(self.skin.selectedBackground)
displayObject.img.selected = true
else
displayObject.img = display.newImage(self.skin.background)
displayObject.img.selected = false
end
displayObject:insert(1, displayObject.img)
end
end
return displayObject

end
[lua] [import]uid: 8000 topic_id: 1691 reply_id: 4961[/import]

Two ideas:

items[numItems] = “test”…numItems should be changed to
items[numItems] = “test”… tostring(numItems)

This way you are adding one string to another.

If you have to use “if data ~= nul” to make it work it means that you are passing a value (data) that is nil. You can add debug code to display the value of variables:
print( tostring(obj) ) – display the value of obj (even if nil)

Usually “nil” valued variables are because they are not defined or out of “scope”. You should read up on Lua basics to help your understanding of the subject if you are still confused.

-Tom [import]uid: 7559 topic_id: 1691 reply_id: 5337[/import]