Jump to content




Error! Bad Argument: String Expected, Got Nil


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

#1 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 15 October 2013 - 03: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

And this is what executes it:

Spoiler


#2 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 15 October 2013 - 04: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?

#3 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 15 October 2013 - 04:12 AM

View Postwojbie, on 15 October 2013 - 04:06 AM, said:

You are trying to reread line number 1 here?
Yes

#4 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 15 October 2013 - 06: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.

#5 Engineer

  • Members
  • 1,378 posts
  • LocationThe Netherlands

Posted 15 October 2013 - 07: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.

#6 MKlegoman357

  • Members
  • 1,170 posts
  • LocationKaunas, Lithuania

Posted 15 October 2013 - 07: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
    


#7 Mackan90096

  • Signature Abuser
  • 518 posts
  • LocationIn my basement.

Posted 15 October 2013 - 11:26 AM

View Postwojbie, on 15 October 2013 - 06:10 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users