This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
orik1997's profile picture

Attempt to index a nil value

Started by orik1997, 21 April 2015 - 02:25 PM
orik1997 #1
Posted 21 April 2015 - 04:25 PM
I am trying to create a program that reads every file in a directory and prints out everything. This is my Code so far: http://pastebin.com//wPHnN0dQ
I get the error attempt to index ? (a nil value) in line 179 I just don't know why my line is defined so I don't know.
SquidDev #2
Posted 21 April 2015 - 04:30 PM
If you look at line 179 you have:

line = file.readLine() -- This function reads the next line in the file, until the end.

Attempt to index nil means in this case that the file variable is nil. This is a result of opening a file that doesn't exist. Line 178 has

file = fs.open("Turtles"..turtleDataFiles[i],"r") -- Open a file for reading.

Turtles should be Turtles/ instead - you are opening a file called TurtlesWhatever instead of Turtles/Whatever.

Also instead of using a for i = 0, … loop you can do:

for _, name in  pairs(fs.list("Turtles/")) do
Edited on 21 April 2015 - 02:31 PM
Quintuple Agent #3
Posted 21 April 2015 - 04:33 PM
Also remember that fs.list returns directories too. So you will need to add a check before you open the file to make sure it is not a directory.
orik1997 #4
Posted 22 April 2015 - 01:13 PM
Ok thanks that worked but apparently I have a bug in my printing function called bigTable it starts at line 99. It should print out everything in every file each file should represent one line that doesn't work it just prints out everything in one line. This is my code: http://pastebin.com/R5hY1ihk
orik1997 #5
Posted 22 April 2015 - 04:40 PM
Ok sorry I found the mistake I forgot to reset the table and I had some other mistakes in there thanks for your help.