Jump to content




[ Error ] [ Solved ] Why is my program returning attempt to call nil?


  • You cannot reply to this topic
14 replies to this topic

#1 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 02:14 PM

Okay so what happens is I run the program and I press enter on the selection 'Programs' and It returns

multi:80: attempt to call nil

I've tried moving the functions that each function needs above each other.

Example:

drawTitle() function needs the space() & cPrint function to work, so I moved those up to the top of the code so It can use those functions

But I still don't know why I get the attempt to call nil when I try use the Programs Selection.

Pastebin link:

http://pastebin.com/8dNJdrRz

Thanks, AnthonyD98

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 02:25 PM

move the declaration of var (line 100) up to the top...

#3 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 02:31 PM

View PostTheOriginalBIT, on 10 February 2013 - 02:25 PM, said:

move the declaration of var (line 100) up to the top...

Then I get the error

multi:53: attempt to index ? (a nil value )

#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 02:42 PM

I get the error on line 51 "'for' limit must be a number... so move numvar up to to the top too...

Also both need to be below mainOptions

edit: There are also problems with this function... gui is declared below it, and your not modifying the var and numvar variables, you have made them local to that scope (function) so it doesn't change the ones in the program scope
local function drawPrograms()
 local var = programOptions
 local numvar = #programOptions
 gui()
end


#5 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 02:56 PM

View PostTheOriginalBIT, on 10 February 2013 - 02:42 PM, said:

I get the error on line 51 "'for' limit must be a number... so move numvar up to to the top too...

Also both need to be below mainOptions

edit: There are also problems with this function... gui is declared below it, and your not modifying the var and numvar variables, you have made them local to that scope (function) so it doesn't change the ones in the program scope
local function drawPrograms()
local var = programOptions
local numvar = #programOptions
gui()
end

Okay now my code looks like this
http://pastebin.com/FsRXifw0

How do I fix the drawPrograms() function
Every place I put it at, it errors.

#6 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 03:09 PM

AHA!

If I write drawPrograms() in a different program as a global function it works!!

EDIT: Now just need to find out how to make it work within the program as a global function!

#7 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 03:17 PM

Now its bugging out again -_-

#8 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 10 February 2013 - 03:28 PM

I highly suggest using an argument within the functions for which table to print etc:

Spoiler


#9 Doyle3694

  • Members
  • 815 posts

Posted 10 February 2013 - 03:29 PM

What do you mean with making it work withing the program as a global function?

#10 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 03:39 PM

View PostremiX, on 10 February 2013 - 03:28 PM, said:

I highly suggest using an argument within the functions for which table to print etc:

Spoiler

If you don't mind could you put that code on pastebin for me?

View PostDoyle3694, on 10 February 2013 - 03:29 PM, said:

What do you mean with making it work withing the program as a global function?

I meant have the drawPrograms() function working within the orginal program and not having to be used outside of the program as a global function

#11 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 10 February 2013 - 03:50 PM

http://pastebin.com/WDFkQJhR

#12 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 03:54 PM

View PostremiX, on 10 February 2013 - 03:50 PM, said:


I saw you changed the center function slighty. What does the col part of it do?
I also noticed whenever I use a function / selection it prints whats happening
Is that a feature that I can turn off and when needed turn on for debugging purposes?

#13 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 10 February 2013 - 04:57 PM

col is for the colour of the background.

Yeah, was a debug line I used:
print(type(_table[s][2])) sleep(1)

You can delete it

#14 AnthonyD98™

  • Members
  • 193 posts
  • LocationAuckland, New Zealand

Posted 10 February 2013 - 05:00 PM

View PostremiX, on 10 February 2013 - 04:57 PM, said:

col is for the colour of the background.

Yeah, was a debug line I used:
print(type(_table[s][2])) sleep(1)

You can delete it

No its fine, I added some tArgs to the beggining of the program.
if you run with -debug the you can see the printing of functions
if you run without the -debug it runs normally.

local tArgs = (...)
if tArgs == "-debug" then
debug = true
else
debug = false
end


#15 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 10 February 2013 - 05:14 PM

This is a more stream line version of what you did
local tArgs = ...
local debug = (tArgs == "-debug")
This can be used because an if statement uses a true false to know what it's to do, so why have an if, to then assign a Boolean... May as well just put the condition in the variable assignment

This is how I normally do it, mind you I normally allow for more args like '-help'
local tArgs = {...}
if #tArgs == 1 then
  if tArgs[1] == "-debug" then
    debug = true
  -- elseif other args checking and such
  else
    -- print usage
  end
elseif #tArgs ~= 0 then -- if they entered something more than 1 arg
  -- print usage
end






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users