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

[Error] Attempt to concatenate a string and nil

Started by Dircome, 29 December 2012 - 02:26 PM
Dircome #1
Posted 29 December 2012 - 03:26 PM
This has me completely confused it really looks like it should work the second argument appears to work but first one doesn't.
  1. local tArgs = { }
  2. local x = 0
  3. local y = 0
  4. local z = 0
  5. local xSize = 0
  6. local zSize = 0
  7. local collected = 0
  8. local unloaded = 0
  9. local idle time = 0
  10. if #tArgs ~= 2 then
  11. print( "Usage: excavate <length>, <width>" )
  12. return
  13. end
  14. print(xSize)
  15. print(zSize)
  16. xSize = tonumber(tArgs[1])
  17. zSize = tonumber(tArgs[2])
  18. print(zSize)
  19. print(xSize)




When i run that code it throws an error and i dont understand why
[attachment=833:2012-12-28_20.13.44.jpg]
Luanub #2
Posted 29 December 2012 - 03:28 PM
remove the extra spaces in your concatenations.

print(xSize .. "\n")

--should be
print(xSize.."\n")
I'm not sure you need the \n print does one for you so this will add an extra line in between the print statements.
Dircome #3
Posted 29 December 2012 - 03:32 PM
Well i tired it like you said but it still throws the same error
Luanub #4
Posted 29 December 2012 - 03:41 PM
Try printing tArgs[1] and see what value it contains. For some reason it looks like the following line is assigning a nil value to xSize. Its weird because tArgs[2] appears to be transferring properly to zSize.

xSize = tonumber(tArgs[1]) --line that does not appear to be working


Try this for the debug prints

for x=1, #tArgs do
  print("tArgs["..x.."] = "..tArgs[x].." and is type "..type(tArgs[x]))
end
Dircome #5
Posted 29 December 2012 - 03:51 PM
It seems that the comma is inserted into the first argument. Thanks for your help how are arguments separated in lua
Lyqyd #6
Posted 29 December 2012 - 04:04 PM
The shell separates arguments by spaces.
Dircome #7
Posted 29 December 2012 - 04:07 PM
Thanks you guys for your help
ChunLing #8
Posted 29 December 2012 - 04:13 PM
Your line numbering does not appear to match the line numbering in the program which is throwing the errors.
Cozzimoto #9
Posted 29 December 2012 - 04:33 PM
first off

 if #tArgs ~= 2 then 

should be something like this to print the usage when you dont have enough arguments

 if #tArgs < 2 then 

second i dont think you can return an empty argument

try
 return nil 
instead or returning nothing
 return 

EDIT:

i didnt even see this that you added a comma when you are trying to pass arguments to your program. the comma makes that a string not a number so when it tried to make a var a number when its a string with a alpha character in it its going to error out
Luanub #10
Posted 29 December 2012 - 04:37 PM
first off

 if #tArgs ~= 2 then 

should be something like this to print the usage when you dont have enough arguments

 if #tArgs < 2 then 

second i dont think you can return an empty argument

try
 return nil 
instead or returning nothing
 return 


if #tArgs ~= 2 then
Works just fine and so will the return.

However it is probably better to error the script then return. Personally I would do this, but its just a matter of personal preference, they both will work.

if #tArgs ~= 2 then
  error( "Usage: excavate <length>, <width>" )
end
Cozzimoto #11
Posted 29 December 2012 - 04:40 PM
i like to be very specific in my programs so i can deduct alot faster where i throw my errors
Dircome #12
Posted 29 December 2012 - 05:12 PM
Sorry I messed it up when i posted it here. My problem was the comma tried to use to seperate my arguements used to java i guess. I copied the try statement out of the excavate program which i am basing my program off of.
ChunLing #13
Posted 29 December 2012 - 08:12 PM
If your issue has been resolved, then all is well. But from what I can see here I do not know that to be the case.

If you are still getting an error, then open the program with edit and go to the line number specified in the error message. Highlight that line in your post.