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

[Lua][Error] Attempt to index ? (a nil value)

Started by Kittychanley, 18 January 2013 - 08:11 PM
Kittychanley #1
Posted 18 January 2013 - 09:11 PM
I am currently trying to program my turtle with misc peripherals to check the status on a gate reader before breaking a thermal expansion energy cell. (Similar to what direwolf20 did in season 3 of his server play around episode 70-ish). What's weird is that sometimes I get the error, and sometimes the program runs just fine. Specifically if I edit the program and resave, it will sometimes work again until I teleport the turtle to another dimension.

I have each step broken up into different programs with a final program doing shell.runs of everything. It seems that each individual program is getting the same error, usually with in the first couple of lines (if I'm correct in that the :#: at the start of the error dictates the line)

Here is the code of one of the individual programs:


shell.run("clear")
local data = {}
m = peripheral.wrap("front")
data = m.get()
print("Discharging")
while data["No Energy"] == false do
     print("Still Discharging")
     sleep(60)
     data = m.get()
end
turtle.turnRight()
turtle.forward()
turtle.turnLeft()
turtle.select(1)
turtle.dig()
turtle.place()
turtle.turnLeft()
turtle.forward()
turtle.turnRight()

The other program is essentially the same, with the movements in a different order and checking "Full Energy" instead of "No Energy".
From what I've read in the error slaying tutorial, I think it has something to do with the table. I just don't know where to start to fix it.
Doyle3694 #2
Posted 18 January 2013 - 09:51 PM
EDIT: Give us error line.
Luanub #3
Posted 18 January 2013 - 09:56 PM
I would try to print the table prior to starting the while loop to check to make sure it always getting something from the m.get(). You may even want to try printing it inside the loop as well.
theoriginalbit #4
Posted 18 January 2013 - 10:53 PM
You could also surround the code ( from line before m.get() to the end with this if statement…

if m.get then
  -- code here
end
This will check that m.get exists before trying to use it…
3ydney #5
Posted 19 January 2013 - 01:14 AM
Your problem is m.get()
There is no peripheral.get() api
Goto the link http://computercraft.info/wiki/index.php?title=Peripheral_(API)
theoriginalbit #6
Posted 19 January 2013 - 01:19 AM
Your problem is m.get()
There is no peripheral.get() api
Goto the link http://computercraft...Peripheral_(API)

misc peripherals to check the status on a gate reader before breaking a thermal expansion energy cell.
Peripherals also add peripheral methods…

EDIT: Also the link you provided is of a page that doesn't have details. you missed the closing )
ChunLing #7
Posted 19 January 2013 - 03:32 AM
Hmm…but not every peripheral is going to add that particular method. So it's worth checking.

We probably do need a specific error line and the code which generated that error line, to be of any real help.
theoriginalbit #8
Posted 19 January 2013 - 03:34 AM
Hmm…but not every peripheral is going to add that particular method. So it's worth checking.

thats why i suggested checking m.get first making it

data = m.get and m.get()
ChunLing #9
Posted 19 January 2013 - 03:40 AM
Ah, my bad. Though really, that doesn't help much, since you should probably return false out of the program if not m.get, there being no point in continuing.
theoriginalbit #10
Posted 19 January 2013 - 03:43 AM
Ah, my bad. Though really, that doesn't help much, since you should probably return false out of the program if not m.get, there being no point in continuing.
yeh you could have an

if not data then
  print("Cant get")
  error()
end
to error out if it hasn't read anything.
Kittychanley #11
Posted 19 January 2013 - 03:19 PM
Sorry, I had forgotten the line the error was in. I reran the program and it is in line 4, so the data = m.get() is where it is getting the nil value. I've made sure that the "front" of my turtle is facing the gate reader, which according to the misc peripherals page found here:

http://www.computerc...peripherals-23/

has the get() method. [Exposes a single function, get() which returns all gate conditionals as a string-boolean table]

I have tried running my gate reader test program, and now it is getting an error on the line with data = m.get() as well.


m = peripheral.wrap("front")
data = m.get()
for i, j in pairs(data) do
print(tostring(i)..": "..tostring(j))
end

What is mind boggling is that this test used to work and print the table, so I don't know if something was changed with misc peripherals that the method is no longer working.
Proof that at one point this code worked: http://www.youtube.com/watch?feature=player_detailpage&v=Fej5qrcyh_Y#t=1413s

I'm still very new to programming and it would greatly help if you could tell me the line and exact place to put things into the code to test/fix it.
ChunLing #12
Posted 19 January 2013 - 03:28 PM
Try using something like:
for k,v in pairs(m) do
    print(k,": ",type(v))
end
This will get you an idea of which methods are available.

Why in particular this isn't working may have something to do with the turtle turning between the time that a peripheral connection event and you trying to use the peripheral. Try making it so that, once your turtle moves into contact with a peripheral, it doesn't turn before wrapping/using the peripheral.
theoriginalbit #13
Posted 19 January 2013 - 03:30 PM
Using the following code, just check what all the functions are available for you to use…

for k,v in pairs( peripheral.getMethods( "front" ) do
  print( k.." : "..type( v )  )
end
Then if get is there try this

m = peripheral.wrap( "front" )
print( m.get )
No the missing ( ) is not a typo, this should print out "function: <random number", if it says that then the function is definitely there and available for use…
Kittychanley #14
Posted 19 January 2013 - 03:47 PM
And I feel like a complete idiot. Thank you guys so much for your help, but I had a redstone energy cell placed where my gate reader should be. They sure look similar. :huh:/> The m.get() function was indexing a nil value because energy cells, unlike gate readers, have no peripheral wrap or get() method. I switched the gate reader and the energy cell and everything works perfectly. Also explains why it worked on the recharge of the system, but the discharge side gave the error. Once again, thank you guys for helping me!