Error with Table and TableView

I’m writing a really simple program that basically generates URLs and stores a history of them.  I store the data in a table, and present it with a tableView.  Everything works perfectly, but (you knew that but was coming, right?) there is one case that generates an error.

If I open the history, and execute the “clear history” function, the table and tableview objects both clear.  However, if I scroll down the history first (even one line), then run the same “clear history” function, I get an error:

<error>

Corona Runtime Error

?:0: attempt to index field ‘?’ (a nil value)

stack traceback:

?: in function <?:1319>

(tail call): ?

history.lua:109: in function <history.lua:103>

?: in function <?:169>

</error>

Here is the code I’m executing:

<code>

for i = #historyList,1,-1 do 

historyTable:deleteRows ({i},{slideUpTransitionTime = 100}) 

table.remove(historyList,i)

end

</code>

The error happens when the first line inside the for loop is run for the first time.  The crazy thing is, if I just reload the program, with no changes to app or files, and run the program, the “clear history” works great!  If I scroll, crash.  If I don’t scroll, success?

Any ideas?  I’ve been beating on this for weeks, because the crashes seemed so random.  Finally (this week) figured out the steps to duplicate the issue, but I just don’t see a way to resolve the problem.

you need to provide more code if you want help.

there is nothing here about the scroll down step

If I got you right, you may need to slide in an if check before the for loop. It should look something like this:

if (#historyList \> 0) then for i = #historyList,1,-1 do historyTable:deleteRows ({i},{slideUpTransitionTime = 100}) table.remove(historyList,i) end end

 or you can apply this check to your :deleteRows() function.

Why not just use the object:deleteAllRows() function for the tableView (https://docs.coronalabs.com/api/type/TableViewWidget/deleteAllRows.html) and then just nil the data table

historyTable:deleteAllRows() historyList = nil

Rob

Thanks all for the responses, especially Rob Miracle!  The first line from your response fixed it.  I can’t use the second line - my List can’t be nil, but using my loop to clear it, and then the deleteAllRows to clear the TableView, worked perfectly!  Thanks again!

you need to provide more code if you want help.

there is nothing here about the scroll down step

If I got you right, you may need to slide in an if check before the for loop. It should look something like this:

if (#historyList \> 0) then for i = #historyList,1,-1 do historyTable:deleteRows ({i},{slideUpTransitionTime = 100}) table.remove(historyList,i) end end

 or you can apply this check to your :deleteRows() function.

Why not just use the object:deleteAllRows() function for the tableView (https://docs.coronalabs.com/api/type/TableViewWidget/deleteAllRows.html) and then just nil the data table

historyTable:deleteAllRows() historyList = nil

Rob

Thanks all for the responses, especially Rob Miracle!  The first line from your response fixed it.  I can’t use the second line - my List can’t be nil, but using my loop to clear it, and then the deleteAllRows to clear the TableView, worked perfectly!  Thanks again!