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

[LUA] [ERROR] concatenate nil and string?

Started by Tycoonier, 07 October 2012 - 05:15 PM
Tycoonier #1
Posted 07 October 2012 - 07:15 PM
Throwing up this weird error.
Error:
Spoiler

Code:

w,h = term.getSize()


lineno = 1

rednet.open("back")


function listenForMessages()

while true do

local id, msg = rednet.receive()

if lineno == 16 then lineno = 1 end

local oldx,oldy = term.getCursorPos()

term.setCursorPos(1, lineno)

term.clearLine()

if string.find(msg, "::") then

term.write(string.gsub(msg, "::", ":"))

else

term.write(id..":"..msg)

end

term.setCursorPos(oldx, oldy)

end

lineno = lineno + 1

end


function sendMessages()

term.setCursorPos(1, h-1)

while true do

message = io.read()

if message == "quit" then break end

rednet.broadcast(os.getComputerLabel().."::"..message) <–I believe it is this that is causing the error.

term.setCursorPos(1, lineno)

term.write(os.getComputerID()..":"..message)

lineno = lineno + 1

term.setCursorPos(1, h-1)

term.clearLine()

end

end


term.clear()

term.setCursorPos(1, h-2)

term.write(string.rep("*", w))


parallel.waitForAny(sendMessages,

listenForMessages)
Orwell #2
Posted 07 October 2012 - 07:18 PM
Throwing up this weird error.
Error:
Spoiler

Code:

w,h = term.getSize()

lineno = 1
rednet.open("back")

function listenForMessages()
while true do
local id, msg = rednet.receive()
if lineno == 16 then lineno = 1 end
local oldx,oldy = term.getCursorPos()
term.setCursorPos(1, lineno)
term.clearLine()
if string.find(msg, "::") then
term.write(string.gsub(msg, "::", ":"))
else
term.write(id..":"..msg)
end
term.setCursorPos(oldx, oldy)
end
lineno = lineno + 1
end

function sendMessages()
term.setCursorPos(1, h-1)
while true do
message = io.read()
if message == "quit" then break end
rednet.broadcast(os.getComputerLabel().."::"..message) <–I believe it is this that is causing the error.
term.setCursorPos(1, lineno)
term.write(os.getComputerID()..":"..message)
lineno = lineno + 1
term.setCursorPos(1, h-1)
term.clearLine()
end
end

term.clear()
term.setCursorPos(1, h-2)
term.write(string.rep("*", w))

parallel.waitForAny(sendMessages,
listenForMessages)

os.getComputerLabel() will return nil if the computer has no label. So in this case

rednet.broadcast(os.getComputerLabel().."::"..message)
will concatenate a nil value with a string. In front of this line, you might wanna put:

local label = os.getComputerLabel() or ""
and use the variable 'label' from there on.
Doyle3694 #3
Posted 07 October 2012 - 08:10 PM
not sure but think you need () after function names in parallel too.
Kingdaro #4
Posted 07 October 2012 - 08:11 PM
not sure but think you need () after function names in parallel too.
You don't.

You're passing function names, not function calls.