Jump to content




index expected, got nil on function


  • You cannot reply to this topic
1 reply to this topic

#1 LewisTehMinerz

  • Members
  • 174 posts
  • LocationMinecraft in Minecraft in Minecraft in ComputerCraft... in Minecraft

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?

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

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.





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users