attempt to concatenate field '3' (a nil value) error

Started by TheTinyDeskEngineer, Mar 12, 2021, 03:20 AM

Previous topic - Next topic

TheTinyDeskEngineer

I'm working on a program which will allow me to have a computer act as a server for uploading and downloading files, but the line
monitor.write("Sender ID: " .. senderID .. " Request: " .. message[1] .. " arg1: " .. message[2] .. " arg2: " .. message[3])keeps giving the error "startup.lua:92: attempt to concatenate field '3' (a nil value)" and I don't know why. Shifting the numbers in the "message[<number>]" parts back by one only gives the same error but instead it's "...concatenate field '0'" instead, and every time I do it I'm giving it the same inputs.

Lupus590

Lua arrays (well actually tables) start from 1, not 0. So you will probably never want to index the 0th field of a table.

Without knowing how message gets set I can't give you any information other than message[3] is nil.

Post your code, if it's particularly long then upload it to pastebin (or a simular service) and then post a link to it.

TheTinyDeskEngineer

Quote from: Lupus590 on Mar 12, 2021, 04:57 AMLua arrays (well actually tables) start from 1, not 0. So you will probably never want to index the 0th field of a table.

Without knowing how message gets set I can't give you any information other than message[3] is nil.

Post your code, if it's particularly long then upload it to pastebin (or a simular service) and then post a link to it.
here's the code where the values in the list are defined and the message to the host server is sent:
if tArgs[5] == nil then tArgs[5] = "none" end
if tArgs[6] == nil then tArgs[6] = "none" end
rednet.send(serverID, {tArgs[4], tArgs[5], tArgs[6]}, tArgs[3])
tArgs[4] should always be a string, as the program which sends this message will throw an error if the amount of arguments filled is less than the minimum required and thus will never be sent as a non-string as there is nothing in the code turning it into some other type
tArgs[5] will get turned into "none" if it is nil, as well as tArgs[6]

TheTinyDeskEngineer

I added a bit of code to the end of both programs (the filehostservice.lua file, and the startup.lua file for the host) which prints out the stuff that's causing problems.
when I run filehostservice top 0 fhs upload /filehostservice.lua to upload the filehostservice.lua file to to the server, it prints out this without any error:
top --the side which the wireless modem is opened on
0 -- the server ID
fhs -- the protocol used to communicate with the server
upload -- the operation, in this case uploading a file, this is what message[1] should be
/filehostservice.lua -- the file to upload, this is also what message[2] should be
none -- this is arg2, it's unused by the upload mode, so it's nil, but get's turned into "none"

however, when the server completes the request (it does save the file properly, but it deletes the hosted files on startup to prevent problems with missing refrences) it prints this and gives the error mentioned above:
1 -- the sender's computer ID
nil -- this should be "upload" but it isn't
nil -- this should be "/filehostservice.lua" but it also isn't
nil -- this is arg2, unused, so it should be "none" but it's nil

however, the monitor for showing user requests prints the requests out perfectly fine.
the text on the monitor is:
Sender ID: 1 request: upload arg1: /filehostservice.lua arg2: nonethe arguments are printed on the terminal after the line which causes the error, and yet it works perfectly fine, prints the (incorrect) arguments, and then gives the same error, same line.

I changed it so that the startup.lua argument printing happens before the error and now it prints:
nil -- ???
nil -- ???
nil -- ???
1 -- presumably user ID
nil -- ???
nil -- ???
and it still gives the same error.

TheTinyDeskEngineer

Now I changed startup.lua to have the argument printing work with multiple print calls instead of a for loop going through each argument, and it seems to actually have worked, it's showing the text of the file I uploaded, but it also still gave that same error.
I'm going to check the text in the saved file.

The text is just "none".

Lupus590

#5
When I said post your code I meant for you to post all the code that you are running such that I could run it myself if I wanted to. I can't help you debug if I don't know what your code is doing.

TheTinyDeskEngineer

I uploaded the two programs to pastebin.com:
filehostservice.lua: pastebin get FBFMXtEk
startup.lua: pastebin get 4CWe2P25

Lupus590

Your message is dependent on the args that are given to the filehostservice program, if none is provided then it's set to nil. You should add some nil checks so that you avoid these kinds of errors.

TheTinyDeskEngineer

I have two if statements right before the message containing the different command arguments which check if the specific argument is nil, when it will then set it to the string "none" before the message gets sent.

Also, for some reason, despite the terminal recieving only nil as well as the sender ID in the message, the monitor writes the text properly, responding with arg2 being "none" instead of nil, as well as the correct command and filepath.