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

Nil Value In Highly Difficult/Most likely worst way to do something code

Started by Nintendopup, 17 June 2014 - 12:18 AM
Nintendopup #1
Posted 17 June 2014 - 02:18 AM
Okay, well… I know the title is a bit confusing in itself, but really, it is the best that I can describe the issue I'm having. I'm working on something that loosely resembles a button API (Which I may just try to learn how to make). Basically, I'm trying to have icons displayed, x and y coordinates stored in a table, and then use those coordinates to detect if a user clicks on the icons. When they click on the icons, an app is opened (the app's name is stored in the same table as the coordinates). I know, it sounds confusing…and, truthfully, it is. Most likely, I'll just use a button API that already made (or look at more tutorials on how to make one), but I wanted to use what I know already to try this… and, unfortunately, it seems that I've failed :mellow:/> . But, in any case, I'd like to know what I'm doing incorrectly, if anything, or if it's just not possible to do what I want with the setup I have here.

The error I'm getting is:
[string "test"]:46: attempt to index a nil value

On line 46, I have:
if x >= appsTable[1] and x < appsTable[2] and y >= appsTable[3] and y < appsTable[4] then


appsTable = {}
aTn = 1

function UIBlockAdvanced(animatesetting,startposx,endposx,startposy,endposy,Bg,tablevalue)

if animatesetting == "animate" then

aTn = aTn + 1
table.insert(appsTable,aTn,tablevalue)

u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  sleep(0.00)
  u = u + 1
until u == endposy

elseif animatesetting == "normal" then

aTn = aTn + 1
table.insert(appsTable,aTn,tablevalue)

u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  u = u + 1
until u == endposy
end
end

function findPoint()
for i = 1,aTn do
    event, button, x, y = os.pullEvent("mouse_click")
  if x >= appsTable[i][1] and x < appsTable[i][2] and y >= appsTable[i][3] and y < appsTable[i][4] then
   shell.run(appsTable[i][5])
   aTn = 1
  end
end
end

local function drawDesktop()
term.setBackgroundColor(userBg)
term.clear()

term.setBackgroundColor(userNotification)
term.setCursorPos(1,h)
term.clearLine()
term.setCursorPos(1,h-1)
term.clearLine()
UIBlock("normal",1,4,h-1,h+1,userTc)
term.setBackgroundColor(userBg)
term.setCursorPos(2,h)
term.write(" ")
m = 2
z = 6
y = 2
l = 5
o = 1
e = 1
newY = 2
newL = 5
for _, file in ipairs(fs.list("core")) do
if o > 13 then
o = 1
end
if e > 16 then
  m = 22
  z = 26
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/"..file})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
  newY = 2
  newL = 5
elseif e > 12 then
  m = 17
  z = 21
  UIBlockAdvanced("normal",m,z,newY,newL,colorTable[o],{m,z,newY,newL,"core/"..file})
  newY = newY + 4
  newL = newL + 4
  o = o + 1
  e = e + 1
  y = 2
  l = 5
elseif e > 8 then
  m = 12
  z = 16
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/"..file})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
  newY = 2
  newL = 5
elseif e > 4 then
  m = 7
  z = 11
  UIBlockAdvanced("normal",m,z,newY,newL,colorTable[o],{m,z,newY,newL,"core/"..file})
  newY = newY + 4
  newL = newL + 4
  y = 2
  l = 5
  o = o + 1
  e = e + 1
else
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/"..file})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
end
end
end
local function desktopListen()
while true do
    event, button, x, y =os.pullEvent("mouse_click")
    if x == nil or y == nil then
		  cPrint("What")
    end
  if x>0 and x<4 and y>h-2 and y<h+1 then
   break
    else
   findPoint()
   appsList = {}
   drawDesktop()
  end
end
end
drawDesktop()
desktopListen()

All I want to know is what exactly the nil value is (Really, I'm confused). I've tried everything I can, but just can't seem to get it to work. I'd greatly appreciate any help that all of you can give. Sorry if this is too confusing… I don't really know how to explain it any better :/ Anyways, thanks for you help!

- Nin
cptdeath58 #2
Posted 17 June 2014 - 02:39 AM
The Error means that you have a nil value for a table.

if x >= appsTable[i][1] and x < appsTable[i][2] and y >= appsTable[i][3] and y < appsTable[i][4] then
Suggesting that your

appsTable[i][1]
Was not given a value…
Edited on 17 June 2014 - 12:42 AM
Bomb Bloke #3
Posted 17 June 2014 - 02:46 AM
More specifically, that appsTable was not given a value.

Consider: aTn starts as 1. You add one to that for every entry you insert into appsTable. Thus aTn will always be equal to the number of actual entries in appsTable, plus one.

Well, except for the bit where you set it back to 1 again when someone clicks one of your buttons. I'm not sure what that's about.

Anyway, given that #appsTable returns the number of entries in appsTable, aTn may not be needed.
Nintendopup #4
Posted 17 June 2014 - 03:15 AM
The aTn being set equal to one was nothing, it turns out not to have any effect on it. I changed the code to:


appsTable = {}
colorTable = {colors.red,colors.white,colors.blue,colors.orange,colors.yellow,colors.green,colors.lightBlue,colors.pink,colors.purple,colors.black,colors.lightGray,colors.magenta,colors.brown,colors.cyan,colors.lime}
function UIBlockAdvanced(animatesetting,startposx,endposx,startposy,endposy,Bg,tablevalue)
if animatesetting == "animate" then
table.insert(appsTable,tablevalue)
u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  sleep(0.00)
  u = u + 1
until u == endposy
elseif animatesetting == "normal" then
table.insert(appsTable,tablevalue)
u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  u = u + 1
until u == endposy
end
end
function findPoint()
for i = 1,#appsTable do
    event, button, x, y = os.pullEvent("mouse_click")
  if x >= appsTable[i][1] and x < appsTable[i][2] and y >= appsTable[i][3] and y < appsTable[i][4] then
   shell.run(appsTable[i][5])
  end
end
end
local function drawDesktop()
term.setBackgroundColor(userBg)
term.clear()

term.setBackgroundColor(userNotification)
term.setCursorPos(1,h)
term.clearLine()
term.setCursorPos(1,h-1)
term.clearLine()
UIBlock("normal",1,4,h-1,h+1,userTc)
term.setBackgroundColor(userBg)
term.setCursorPos(2,h)
term.write(" ")
m = 2
z = 6
y = 2
l = 5
o = 1
e = 1
newY = 2
newL = 5
for _, file in ipairs(fs.list("core")) do
if o > 13 then
o = 1
end
if e > 16 then
  m = 22
  z = 26
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/"..file})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
  newY = 2
  newL = 5
elseif e > 12 then
  m = 17
  z = 21
  UIBlockAdvanced("normal",m,z,newY,newL,colorTable[o],{m,z,newY,newL,"core/calculator"})
  newY = newY + 4
  newL = newL + 4
  o = o + 1
  e = e + 1
  y = 2
  l = 5
elseif e > 8 then
  m = 12
  z = 16
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/calculator"})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
  newY = 2
  newL = 5
elseif e > 4 then
  m = 7
  z = 11
  UIBlockAdvanced("normal",m,z,newY,newL,colorTable[o],{m,z,newY,newL,"core/calculator"})
  newY = newY + 4
  newL = newL + 4
  y = 2
  l = 5
  o = o + 1
  e = e + 1
else
  UIBlockAdvanced("normal",m,z,y,l,colorTable[o],{m,z,y,l,"core/calculator"})
  y = y + 4
  l = l + 4
  o = o + 1
  e = e + 1
end
end
end
local function desktopListen()
while true do
    event, button, x, y =os.pullEvent("mouse_click")
    if x == nil or y == nil then
		  cPrint("What")
    end
  if x>0 and x<4 and y>h-2 and y<h+1 then
   break
    else
   findPoint()
   appsList = {}
   drawDesktop()
  end
end
end
drawDesktop()
desktopListen()

I remove aTn, but nothing changed. I get what the message means, but why? In drawDesktop(), I am having the table filled out and the arguments of the function UIBlockAdvanced are all filled out. Supposing I'm filling in the table correctly, everything should work. appsTable should exist as long as i is less than or equal to the amount of keys in the table, right? If the code works like it seems it should, appTable should be filled out with 17 keys (the amount of files in the "core" folder I have). The number of files, technically, shouldn't play a role in anything.
Bomb Bloke #5
Posted 17 June 2014 - 03:22 AM
What's the deal with this function call?:

UIBlock("normal",1,4,h-1,h+1,userTc)
Nintendopup #6
Posted 17 June 2014 - 03:26 AM
Oh, sorry, that's part of the API I was using. I figured that I had no bearing with the issue, but if it helps, here:


function UIBlock(animatesetting,startposx,endposx,startposy,endposy,Bg)
if animatesetting == "animate" then
u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  sleep(0.00)
  u = u + 1
until u == endposy
elseif animatesetting == "normal" then
u = startposy
repeat
  c = startposx
  repeat
   term.setBackgroundColor(Bg)
   term.setCursorPos(c,u)
   term.write(" ")
   c = c+1
  until c == endposx
  u = u + 1
until u == endposy
end
end

UserTc is a variable stored in a file with the value colors.lightGray
UserBg is a variable stored in the same file with the value colors.gray

:)/>
Edited on 17 June 2014 - 01:27 AM
Bomb Bloke #7
Posted 17 June 2014 - 04:01 AM
Hmm. Can't see the problem.

Just to be clear, you're still getting the same error on the same line, yes?

Maybe add some print statements to see where, exactly, "i" is counting up to before the crash occurs.
Nintendopup #8
Posted 17 June 2014 - 04:33 AM
The problem did occur in the same line…But thanks to the idea of adding some print statements, I managed to pinpoint exactly what was going wrong. Interestingly, the issue was not with the code here (well, I did move a drawDesktop() statement to the findPoint() function), but instead, the issue was actually within the other apps that ran once you clicked on an icon. Anyways, thanks for all your help :D/>