Jump to content




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


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

#1 valithor

  • Members
  • 1,053 posts

Posted 04 June 2013 - 04:35 PM

So i am attempting to make a short program that will read a file, if there isn't a file it will make a file and input a series of numbers. This will be used in place of gps in a couple of mining turtle programs i am writing but on line 2 i get "attempt to index ? (a nil value)" i get this while trying to check to see if the first number in the line is a nil value.

r = fs.open("test2","r")
if r.readLine(1)==nil then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end
r = fs.open("test2","r")
x = tonumber(r.readLine(1))
r.close()
x = x+1
s = fs.open("test2","w")
s.writeLine(x)
s.close()
r = fs.open("test2","r")
local x = tonumber(r.readLine(1))
print(x)
s.close()

This is part of the code as i was attempting to get this to work before i actually write the mining program.

#2 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 04 June 2013 - 04:40 PM

readLine doesnt take arguments. It just literally reads the next line, so if line 1 is 'a' and line 2 is 'b' then;
print(r.readLine()) --> a
print(r.readLine()) --> b


#3 valithor

  • Members
  • 1,053 posts

Posted 04 June 2013 - 05:00 PM

so then how would i see if there is a number there or not in the file it is reading?

#4 Bomb Bloke

    Hobbyist Coder

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

Posted 04 June 2013 - 09:05 PM

it still sounds a little like you're confused about what fs.readLine() does.

Anyway, use fs.exists() for this.

if not fs.exists("test2") then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end


#5 valithor

  • Members
  • 1,053 posts

Posted 04 June 2013 - 10:02 PM

View PostBomb Bloke, on 04 June 2013 - 09:05 PM, said:

it still sounds a little like you're confused about what fs.readLine() does.

Anyway, use fs.exists() for this.

if not fs.exists("test2") then
  w = fs.open("test2","w")
  w.writeLine("0")
  w.close()
end

Thank you this is what i was looking for

I know that the fs.readLine() would not make a file but i was looking for a way to see if the file existed, I knew i could use the fs.open("test2","w") to make a file but then i would not know if i was erasing the data inside the file itself, But i didnt know a fs.exist command even existed so i was unable to find a way to see if the file existed.

I have never done any kind of coding in any kind before so a lot of it at first looks like a alien language to me

Edited by valithor, 04 June 2013 - 10:04 PM.


#6 Bomb Bloke

    Hobbyist Coder

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

Posted 05 June 2013 - 06:41 AM

In that case, keep the API list handy. Whenever you want a command for a certain task, take a look there for the API that seems most closely related, see what functions it has for you.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 05 June 2013 - 06:47 AM

View Postvalithor, on 04 June 2013 - 10:02 PM, said:

I have never done any kind of coding in any kind before so a lot of it at first looks like a alien language to me
You should look at a real programming language, then you will see alien :P
Oh or take a look at one that looks alien to experienced programmer.

Bomb Bloke is right though, bookmark the API list in the wiki, also bookmarking some tutorials on lua-users.org or the PIL can come in very handy while programming, and lastly, Google is our friend, 9/10 it can probably answer your questions much quicker (/shamelessplug don't know how to Google efficiently, take a look at my tutorial)... even the best of programmers need reference material from time to time. :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users