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

Window 179 error when calling function

Started by Mr_Programmer, 31 December 2015 - 10:48 PM
Mr_Programmer #1
Posted 31 December 2015 - 11:48 PM
Hi all,

i have this function on 2 programs i made one of them being 2000+ lines which i cant use becouse i keep getting "window: 179: bad argument: string expected:got nil"

i cant find out for the world of me what it is, only happens when this function is called:

function drawSubMenu()
  term.setBackgroundColour(c_writing)
  term.setTextColour(c_menu)
  term.setCursorPos(21,2)
  print("===================")
  term.setTextColour(colourTbl["c_border"])
  term.setCursorPos(21,3)
  print("   Reboot System   ")
  term.setCursorPos(21,4)
  print("				   ")
  term.setCursorPos(21,5)
  print("  Shutdown System  ")
  term.setCursorPos(21,6)
  term.setTextColour(c_menu)
  print("===================")
  term.setCursorPos(31,1)
  term.setTextColour(c_off)
  term.setBackgroundColour(colourTbl["c_error"])
  print("System <<")
term.setBackgroundColour(black)
end 

Any help would be thank full…
Edited on 31 December 2015 - 10:49 PM
Lyqyd #2
Posted 31 December 2015 - 11:59 PM
Post the rest of the code.
Creator #3
Posted 01 January 2016 - 12:00 AM
  1. Are you sure the error happens in this function?
  2. I believe it is not what is causing the error, but are you sure all the term.setText/BackgroundColor functions get numbers as arguments.
  3. None of the other functions should error when passed nil, so I am sure it is not here.
  4. Up to what point does it print.
Dragon53535 #4
Posted 01 January 2016 - 12:04 AM

term.setTextColour(colourTbl["c_border"])
term.setBackgroundColour(colourTbl["c_error"])

These two, are c_border and c_error variables? If so, you need to so it like this:

term.setTextColour(colourTbl[c_border])
term.setBackgroundColour(colourTbl[c_error])

Wouldn't really cause your errror, but hey maybe.
Edited on 31 December 2015 - 11:05 PM
Mr_Programmer #5
Posted 01 January 2016 - 01:53 AM
  1. Are you sure the error happens in this function?
  2. I believe it is not what is causing the error, but are you sure all the term.setText/BackgroundColor functions get numbers as arguments.
  3. None of the other functions should error when passed nil, so I am sure it is not here.
  4. Up to what point does it print.
yes I am certain it is the function becouse once I take away the calling line for the function the program works fine, it doesn't print at all



term.setTextColour(colourTbl["c_border"])
term.setBackgroundColour(colourTbl["c_error"])

These two, are c_border and c_error variables? If so, you need to so it like this:

term.setTextColour(colourTbl[c_border])
term.setBackgroundColour(colourTbl[c_error])

Wouldn't really cause your errror, but hey maybe.
Yes they are both variables but they are stored in a table, it's not that that is giving the error becouse I run a function before that that uses the table for colours, also becouse they are stored in a table I need "" around then to call the key which returns the value, doesn't work when they have no "" around them
Edited on 01 January 2016 - 12:55 AM
Lyqyd #6
Posted 01 January 2016 - 02:08 AM
Post the rest of the code.
Mr_Programmer #7
Posted 01 January 2016 - 02:20 AM

---------------------------------------------------
-- Made By: MrProgrammer
--
-- Website: www.programmersparadise.moonfriut.com
--
--  All code is copyrighted, Charlie Kendall 2015©
--
version = "v1.2.1"
---------------------------------------------------
local colourTbl = {c_header = 16, c_error = 128, c_border = 512}
--c_border = 512
--c_header = 16
c_writing = 1
c_off = 16384
c_on = 32
compDSP = 4
c_menu = 258
--c_error = 128
black = 32768

remoteConnectOption = nil
compName = "Quarantine_Farming ".. version
---------------------------------------------------
function tBar()
term.clear()
term.setCursorPos(1,1)
term.setTextColour(colourTbl["c_header"])
term.setBackgroundColour(colourTbl["c_error"])
term.clearLine()
term.setCursorPos(1,1)
print(compName)
term.setTextColour(c_off)
term.setCursorPos(31,1)
print("System ^^")
term.setBackgroundColour(black)
end

function mainScreen()






















end

function drawSubMenu()
term.setBackgroundColour(c_writing)
term.setTextColour(c_menu)
term.setCursorPos(21,2)
print("===================")
term.setTextColour(colourTbl["c_border"])
term.setCursorPos(21,3)
print("   Reboot System   ")
term.setCursorPos(21,4)
print("                   ")
term.setCursorPos(21,5)
print("  Shutdown System  ")
term.setCursorPos(21,6)
term.setTextColour(c_menu)
print("===================")
term.setCursorPos(31,1)
term.setTextColour(c_off)
term.setBackgroundColour(colourTbl["c_error"])
print("System <<")
term.setBackgroundColour(black)
end

function configCheck()
configFileDir = fs.exists("Farm_Cfg/WirelessControl")
if configFileDir ~= true then
fs.makeDir("Farm_Cfg/WirelessControl")
term.setCursorPos(1,2)
term.clearLine()
term.setCursorPos(1,3)
term.clearLine()
term.setCursorPos(1,2)
print("Are you using the Wireless Addon?")
term.setCursorPos(1,3)
print("true or false:")
term.setCursorPos(15,3)
local userInput = read()
if userInput == "true" then
insertCfg = fs.open("Farm_Cfg/WirelessControl/config", "w")
insertCfg.writeLine("true")
insertCfg.close()
configCheck()
elseif userInput == "false" then 
insertCfg = fs.open("Farm_Cfg/WirelessControl/config", "w")
insertCfg.writeLine("false")
insertCfg.close()
configCheck()
else
term.setCursorPos(1,2)
term.clearLine()
term.setCursorPos(1,3)
term.clearLine()
term.setCursorPos(1,2)
print("Incorrect Statmet, True Or False, Try Again!")
fs.delete("Farm_Cfg")
configCheck()
end
end
readWireless = fs.open("Farm_Cfg/WirelessControl/config", "r")
checkWirelessCfg = readWireless.readLine()
readWireless.close()
if checkWirelessCfg == "true" then
remoteConnect()
remoteConnectOption = true
elseif checkWirelessCfg == "false" then
getFuel()
remoteConnectOption = false
end
end
function remoteConnect()
rednet.open("right")
CMD = rednet.receive()
if CMD == "RUN-x28" then

else

end
end
function getFuel()
fuel = turtle.getFuelLevel()
if fuel > 600 then
term.setCursorPos(1,2)
term.clearLine()
term.setCursorPos(1,3)
term.clearLine()
term.setCursorPos(1,2)
print("Fuel Level: "..fuel)
pos()
else
turtle.select(16)
turtle.refuel(2)
turtle.select(1)
getFuel()
end
end

function rowX28()
for i=1,27 do
if turtle.detectDown() then
  local suc, item = turtle.inspectDown()
  if suc and item.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
else
turtle.down()
local farmland, block = turtle.inspectDown()
if farmland and block.name == "minecraft:farmland" then
turtle.up()
turtle.placeDown()
turtle.forward()
elseif farmland and block.name == "minecraft:grass" or "minecraft:dirt" then
turtle.up()
turtle.select(15)
turtle.equipLeft()
turtle.digDown()
turtle.equipLeft()
turtle.select(1)
turtle.placeDown()
turtle.forward()
end
end
end
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
else
turtle.down()
local farmland, block = turtle.inspectDown()
if farmland and block.name == "minecraft:farmland" then
turtle.up()
turtle.placeDown()
elseif farmland and block.name == "minecraft:grass" or "minecraft:dirt" then
turtle.up()
turtle.select(15)
turtle.equipLeft()
turtle.digDown()
turtle.equipLeft()
turtle.select(1)
turtle.placeDown()
end
end
end

function rowX28MOD()
for i=1,2 do
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
end
end
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
end
turtle.forward()
turtle.forward()
for i=1,5 do
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
end
end
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
end
turtle.forward()
turtle.forward()
for i=1,5 do
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
        end
    end
     if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
end
turtle.forward()
turtle.forward()
for i=1,5 do
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
        end
    end
     if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
end
turtle.forward()
turtle.forward()
for i=1,2 do
if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
          turtle.forward()
          else
          turtle.forward()
            end
        end
    end
     if turtle.detectDown() then
  local suc, t = turtle.inspectDown()
  if suc and t.metadata == 7 then
          turtle.digDown()
          turtle.placeDown()
            end
end 
end

function pos()
turtle.up()
turtle.turnLeft()
turtle.turnLeft()
turtle.forward()
turtle.forward()
farm()
end

function endPos()
turtle.turnRight()
for i=1,17 do
turtle.forward()
end

turtle.turnLeft()
turtle.forward()
turtle.forward()
turtle.down()
turtle.dropDown(64)
turtle.select(2)
turtle.dropDown(64)
turtle.select(3)
turtle.dropDown(64)
turtle.select(4)
turtle.dropDown(64)
turtle.select(5)
turtle.dropDown(64)
turtle.select(6)
turtle.dropDown(64)
turtle.select(7)
turtle.dropDown(64)
turtle.select(8)
turtle.dropDown(64)
turtle.select(9)
turtle.dropDown(64)
turtle.select(10)
turtle.dropDown(64)
turtle.select(11)
turtle.dropDown(64)
turtle.select(12)
turtle.dropDown(64)
turtle.select(13)
turtle.dropDown(64)
turtle.select(14)
turtle.dropDown(64)
turtle.select(1)
sleep(300)
os.reboot()
end

function farm()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28MOD()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28MOD()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
turtle.turnLeft()
turtle.forward()
turtle.turnLeft()
rowX28()
turtle.turnRight()
turtle.forward()
turtle.turnRight()
rowX28()
endPos()
end

tBar()
drawSubMenu()

there is nothing else in the code that is making this error, ive checked everything, like i say the whole program works fine untill i remove the comments from that one function, this is the program I'm working on at the minute, won't bother posting my 2000+ code becouse whatever is wrong with this one will be the same as the other program.
Edited on 01 January 2016 - 01:35 AM
Bomb Bloke #8
Posted 01 January 2016 - 03:27 AM
c_menu = 258

Not a valid colour. You meant 256, but colours.lightGrey would be more readable.
Lyqyd #9
Posted 01 January 2016 - 03:30 AM
If the window API works the same way the term API does, any value from 256 up to 511 will result in the same color. But yeah, colors.lightGray is the best for clarity purposes.
Bomb Bloke #10
Posted 01 January 2016 - 03:36 AM
If the window API works the same way the term API does, any value from 256 up to 511 will result in the same color.

It doesn't. The relevant bits:

    local function setTextColor( color )
        if not parent.isColor() then
            if color ~= colors.white and color ~= colors.black and color ~= colors.gray and color ~= colors.lightGray then
                error( "Color not supported", 3 )
            end
        end
        nTextColor = color  --# Whatever number is passed in gets directly set as the current terminal colour.
        if bVisible then
            updateCursorColor()
        end
    end

--# There's no index 258 in this table:
local tHex = {
    [ colors.white ] = "0",
    [ colors.orange ] = "1",
    [ colors.magenta ] = "2",
    [ colors.lightBlue ] = "3",
    [ colors.yellow ] = "4",
    [ colors.lime ] = "5",
    [ colors.pink ] = "6",
    [ colors.gray ] = "7",
    [ colors.lightGray ] = "8",
    [ colors.cyan ] = "9",
    [ colors.purple ] = "a",
    [ colors.blue ] = "b",
    [ colors.brown ] = "c",
    [ colors.green ] = "d",
    [ colors.red ] = "e",
    [ colors.black ] = "f",
}

    function window.write( sText )
        sText = tostring( sText )
        internalBlit( sText, string_rep( tHex[ nTextColor ], #sText ), string_rep( tHex[ nBackgroundColor ], #sText ) )  --# This is where the error gets thrown; string.rep is expecting a string but gets passed nil.
    end

I'm tempted to file this as a CC bug; ideally the set colour function would figure out the closest power of two and set to that, or just error or something. It'd make mistakes like Mr_Programmer's rather more obvious.

On the other hand, correct usage results in no issues at all, so…
Edited on 01 January 2016 - 02:38 AM
Mr_Programmer #11
Posted 01 January 2016 - 03:59 AM
If the window API works the same way the term API does, any value from 256 up to 511 will result in the same color.

It doesn't. The relevant bits:

    local function setTextColor( color )
        if not parent.isColor() then
            if color ~= colors.white and color ~= colors.black and color ~= colors.gray and color ~= colors.lightGray then
                error( "Color not supported", 3 )
            end
        end
        nTextColor = color  --# Whatever number is passed in gets directly set as the current terminal colour.
        if bVisible then
            updateCursorColor()
        end
    end

--# There's no index 258 in this table:
local tHex = {
    [ colors.white ] = "0",
    [ colors.orange ] = "1",
    [ colors.magenta ] = "2",
    [ colors.lightBlue ] = "3",
    [ colors.yellow ] = "4",
    [ colors.lime ] = "5",
    [ colors.pink ] = "6",
    [ colors.gray ] = "7",
    [ colors.lightGray ] = "8",
    [ colors.cyan ] = "9",
    [ colors.purple ] = "a",
    [ colors.blue ] = "b",
    [ colors.brown ] = "c",
    [ colors.green ] = "d",
    [ colors.red ] = "e",
    [ colors.black ] = "f",
}

    function window.write( sText )
        sText = tostring( sText )
        internalBlit( sText, string_rep( tHex[ nTextColor ], #sText ), string_rep( tHex[ nBackgroundColor ], #sText ) )  --# This is where the error gets thrown; string.rep is expecting a string but gets passed nil.
    end

I'm tempted to file this as a CC bug; ideally the set colour function would figure out the closest power of two and set to that, or just error or something. It'd make mistakes like Mr_Programmer's rather more obvious.

On the other hand, correct usage results in no issues at all, so…
OMG, I just realised after reading your post Bomb_bloke, why is it 258 it should be 256, CC should really tell you what the error is like mine was just a simple typo but when ahead and told me stuff todo with window API which baffled my head, I'm not at my computer right now, will try it out and let you know, thanks for the help!