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

index expected, got nil on function

Started by LewisTehMinerz, 08 January 2015 - 07:38 AM
LewisTehMinerz #1
Posted 08 January 2015 - 08:38 AM
Hi, I have this code:


function xml:create( file ) -- Generates a base XML file which is compatible with this parser
f = fs.open( file, "w" )
if f then
  f.write("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n")
  f.write("<CC>\n")
  f.write(" <!-- Please put all your XML inside the <CC> tags! Thanks! -->\n")
  f.write("</CC>")
  f.close()
  return true
else
  return false
end

I've tried commenting out the "if f then" blocks.

But it just says:
xml:10: index expected, got nil

Line 10 is function xml:create( file ). (I cut some of it out.)

The other file calls it using bool = xml:create( "test" )
but it just does it again.

Help?
Bomb Bloke #2
Posted 08 January 2015 - 08:54 AM
Sounds like you never defined "xml" as a table. Presumably you want something like this somewhere higher up in the code:

local xml = {}

Or, if this script is a file you intend to load as an API, you should probably be declaring the function as just:

function create( file )

… as os.loadAPI() will create the "xml" table and dump the function in there for you. You'd then call it like xml.create("test").

If you really wanted to call it as xml:create("test"), then you'd construct it like this:

function create( self, file )

… but that seems kinda pointless in my opinion.

FWIW, there's also a "writeLine" function available to write-mode file handles.