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

Error! Bad Argument: String Expected, Got Nil

Started by Mackan90096, 15 October 2013 - 02:48 AM
Mackan90096 #1
Posted 15 October 2013 - 04:48 AM
Hi there!

I'm working on my ccSQL project.
I get the error: ccsql:50: bad argument: string expected, got nil

Line 50 is in this case line 16.
Code for the function:
Stupid forum, breaking my indentation :(/>

Spoiler


function select(what,dbname,equals)
dbName = tostring(dbname)
c1 = tostring(what)
c2 = tostring(equals)

if fs.exists(dbName) then
f = fs.open(dbName, "r")
line = f.readAll()
if c1 == "*" then
print(line)
f.close()
else
if string.find(line, c1) then
pos = string.find(line, c1)
lenght = f.readLine(1)
leng = string.len(lenght)
found = true
v1 = string.sub(line, leng+1, string.len(c1)) 
v2 = string.sub(line, leng+3,string.len(c2))
print("Found!")
print("What: "..v1.." Value: "..v2)
print(line)
f.close()
else
print("No matches")
f.close()
end
end
else
print("No such database")
end
end

And this is what executes it:

Spoiler

os.loadAPI("ccsql")

ccsql.select("Name", "Test", "Hai", "Nop")
Wojbie #2
Posted 15 October 2013 - 05:06 AM
in line 8 - line = f.readAll() - that reads whole file - leaves marker on eof

line 15 - lenght = f.readLine(1) - attempts to read next line after marker - but there is nothing cause readAll read it all - gets eof - returns nil
line 16 - leng = string.len(lenght) - tries to get length on nil - error.


Also i don't think that readLine accepts any arguments?If it does its not documented in wiki. It just reads next line after marker. You are trying to reread line number 1 here?
Mackan90096 #3
Posted 15 October 2013 - 05:12 AM
You are trying to reread line number 1 here?
Yes
Wojbie #4
Posted 15 October 2013 - 07:10 AM
2 ways to sole it Adwanced and Easy

Easy - simply close and reopen file - that will rester market and readline would restart from beginning of file
So to get to 5 line simply readline 5 times and last one gives you 5th line.

Advanced - you readAll or readLine whole file store it in table or somewhere and work on table instead of file.
Engineer #5
Posted 15 October 2013 - 08:21 AM
This is a proper way of reading a file line by line:

local lines = {}

local file = fs.open( "file", "r" )

for line in file.readLine do
	table.insert( lines, line )
end

file.close()
This way you can easily find your lines; lines[ 1 ] is the first line. Now, you should keep this table for later one, so you dont have to open unneccesarily files.
MKlegoman357 #6
Posted 15 October 2013 - 08:59 AM
Tips:

  • Localize variables (you should know what it is and why you should do it).
  • Next time when pasting your code, indent it after you paste it.
  • You can use # to get length of strings and tables:
  • 
    local text = "Hi"
    local info = {"Yes", 2}
    
    print(#text)
    -->> 2
    
    print(#info)
    -->> 2
    
    print(#info[1])
    -->> 3
    
Mackan90096 #7
Posted 15 October 2013 - 12:26 PM
2 ways to sole it Adwanced and Easy

Easy - simply close and reopen file - that will rester market and readline would restart from beginning of file
So to get to 5 line simply readline 5 times and last one gives you 5th line.

Advanced - you readAll or readLine whole file store it in table or somewhere and work on table instead of file.

I'll do it the Easy way. Thanks! :D/>