Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
24KaratCarrot

Getting error "bad argument #1 (number expected, got string):"

Question

I'm trying to port over a library I made for Computer Craft, but I can't tell for the life of me where this error is coming from.

I know that the library works properly, as I've gotten another of it's functions working just fine. Sorry if this is a noob question, I am a java developer, and am just getting into lua.

 

Full error message:

bad argument #1 (number expected, got string):
stack traceback:
    [C]: in function 'error'
    machine:986: in function <machine:983>
    (...tail calls...)
    /lib/fileMngr.lua:10: in function 'sendFile'
    /home/send:3: in main chunk
    (...tail calls...)
    [C]: in function 'xpcall'
    machine:751: in function 'xpcall'
    /lib/process.lua:63: in function </lib/process.lua:59>

fileMngr.sendFile:

 1 local manager = {}
 2 
 3 local fs = require("filesystem")
 4 local component = require("component")
 5 local event = require("event")
 6 
 7 local modem = component.modem
 8 local SENDPORT = 1
 9 
10 function manager.sendFile(fileName, address)
11 	 inFile = fs.open(fileName, 'r')
12   modem.open(SENDPORT)
13   modem.send(address, SENDPORT, fileName)
14   os.sleep(2)
15   fileString = infile:read()
16   modem.send(address, SENDPORT, fileString)
17   modem.close(SENDPORT)
18 end
19
20 -- ... The library goes on
21
22 return manager

send.lua (simple test program):

fm = require("fileMngr")
address = "<the receiving modem's address>"
fm.sendFile("test", address)

 

Edited by 24KaratCarrot
Somehow an extra s ended up in address
Link to post
Share on other sites

4 answers to this question

Recommended Posts

  • 0
27 minutes ago, Molinko said:

address is misspelled as 'addresss'. too many s... That was surprisingly hard :P 

Man, I wish this were the mistake! I don't know how that s got there, but it happened between my source and posting it here, the original code has address spelled correctly. But I'm surprised you caught that, I read over everything after I posted and didn't catch it! I've edited the question so that it's spelled correctly, but the problem still persists.

Link to post
Share on other sites
  • 1

I have an idea™... I think your error may be from the filesystem api file handle... It's just a hunch that when 

4 hours ago, 24KaratCarrot said:

infile:read()

is called passing(or not passing...) nil somehow results in a string being used hence the error..

bad argument #1 (number expected, got string)

The doc recommends using the 'io' library instead of the 'filesystem' lib because it has buffered streams. Perhaps some fuckery is afoot.. I'd say try this and report back ;).

 1 local manager = {}
 2 
 3 local fs = require("filesystem") -- # maybe you don't need this anymore?..
 4 local component = require("component")
 5 local event = require("event")
 6 
 7 local modem = component.modem
 8 local SENDPORT = 1
 9 
10 function manager.sendFile(fileName, address)
11 	 inFile = io.open(fileName, 'r')
12   modem.open(SENDPORT)
13   modem.send(address, SENDPORT, fileName)
14   os.sleep(2)
15   fileString = infile:read('*a')
16   modem.send(address, SENDPORT, fileString)
17   modem.close(SENDPORT)
18 end
19
20 -- ... The library goes on
21
22 return manager

This uses the 'io' lib instead. Also note the '*a'(readall from buffer) arg passed to infile:read.

Hope this helps :) 

Link to post
Share on other sites
  • 0
  • Solution
1 hour ago, Molinko said:

 1 local manager = {}
 2 
 3 -- # local fs = require("filesystem")
 4 local component = require("component")
 5 local event = require("event")
 6 
 7 local modem = component.modem
 8 local SENDPORT = 1
 9 
10 function manager.sendFile(fileName, address)
11 	 inFile = io.open(fileName, 'r')
12   modem.open(SENDPORT)
13   modem.send(address, SENDPORT, fileName)
14   os.sleep(2)
15   fileString = infile:read('*a')
16   modem.send(address, SENDPORT, fileString)
17   modem.close(SENDPORT)
18 end
19
20 -- ... The library goes on
21
22 return manager

This uses the 'io' lib instead. Also note the '*a'(readall from buffer) arg passed to infile:read.

Hope this helps :) 

I think the inFile:read() thing was the problem, as when I changed that, the error changed. I honestly don't know exactly what fixed the problem, but I got it working and cleaned things up a bit! I think it was the read("*a") and requiring io, but I'm not  entirely sure.Thanks a bunch for your help, here's the final product:

local manager = {}

local component = require("component")
local event = require("event")
local io = require("io")

if not component.isAvailable("modem") then
  error("No compatable network card found.")
end

local modem = component.modem
local SENDPORT = 1

function manager.sendFile(fileName, address)
  print("Preparing to send file...")
  modem.open(SENDPORT)
  local inFile = io.open(fileName, 'r')
  local fileString = inFile:read("*a")
  os.sleep(2)
  modem.send(address, SENDPORT, fileName)
  inFile:close()
  print("Sending file...")
  modem.send(address, SENDPORT, fileString)
  modem.close(SENDPORT)
  print("File sent.")
end

-- The library goes on

return manager

 

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.