Being updated for 1.3; Some information is incorrect for 1.21, e.g getting line #s all the time
Hello, and welcome to the Codex of Error Slaying.
In this tutorial, I will be detailing various species of errors, and how to slay them.
If your particular species of error is not here, please reply, post your error, and your entire code (if you do not want to share it, you can PM it to me). Please don't post "just the segment I think the error is in", as I can't run it :)/>/> )
If you are unfamiliar with lua, please have a look at Programming In Lua
- Syntaxical Errors
- Multiple Points
- TODO these.
- Lucily, you usually get a line number here.
- Casper's debugger May not work for 1.3 This will allow you to get a stack trace + the line where your error happened, if you did not get it. You will always get a line # of the error in 1.3; No stack trace, however.
What this error means: You are trying to call a variable that is nil:
undefinedFunction() -- This will error.
myFunctions = {}
myFunctions.undefinedFunction() -- This will also error.
In most cases, this is because of a spelling mistake, and/or incorrect capitalization:
print(toString(20)) -- This will error; All lua standard libraries use all-lowercase functions
print(tostring(20)) -- This will work.
term.setcursorpos(5, 10) -- This will error; Computercraft uses camelCase:
term.setCursorPos(5, 10) -- This will work.
"But I've made the function! I know it exists!”
You've probably defined your function after you try to use it!
Here's an example:
sayHello("World")
function sayHello(str)
print("Hello, " .. str)
end
To fix it, simply move your function up (since values are not data, you can do this, even if you call the function in another function; see Chapter 1 for more details)
function sayHello(str)
print("Hello, " .. str)
end
sayHello(“World”)
What this error means: You are trying to get something from nil:
myTable = nil
myTable[20] -- Will error; myTable is nil!
This is very common, especially if you have another function passing a table to one using it:
function getImportantValue(tTable)
return tTable.importantValue
end
local iHaveAnImportantValue = {importantValue = 20}
getImportantValue(iHaveAnlmportantValue) -- This calls getImportantValue without the table,
-- getImportantValue is not expecting this!
This is not an "easy" error to track down, especially without an error number.
The best we can do is locate where something is going wrong:
Add checks to your functions, error (with useful info!) if wrong arguments are passed.
Check that things you expect to be tables actually are tables.
This will not "fix" the error, but it will help you locate it. (which ends up being either a typo, wrong argument, forgotten argument, or you expected something to be a table, but it wasn’t (e.g using io.open, but the file can’t open), or a myriad of other problems.)
] Expected, Got [Boolean/Function/Nil/Number/String/Table
] [acronym="Return to Index"]^[/acronym]
What does this mean?
One of the functions you're calling is erroring, because it recieved the wrong types of arguments!
Example:
function getRoman()
return {i = 1, v = 5, x = 10, l = 50, c = 100, d = 500, m = 1000}
end
print(string.sub(getRoman(), 2, 8)
You'll get a line number, however.
If you go to that line, you can print the types and values of all the varaibles:
function getRoman()
return {i = 1, v = 5, x = 10, l = 50, c = 100, d = 500, m = 1000}
end
-- Line that errors:
print(string.sub(getRoman(), 2, 8)
--Without seeing what myFunction is returning, this can be confusing; so let's print it:
local gRResult = getRoman()
print("gRResult: ", gRResult, " type: ", type(gRResult))
print(string.sub(gRResult, 2, 8)) -- This will still error, but we can see what went wrong now!
Once we determine that we're getting the wrong arguments, we can fix that:
function getRomanNumerals() -- Change the name of our old function.
return {i = 1, v = 5, x = 10, l = 50, c = 100, d = 500, m = 1000}
end
function getRomanAlphabet() -- Take the same precaution with what we intended to do.
return "ABCDEFGHIKLMNOPQRSTVXYZ"
end
print(string.sub(getRomanAlphabet(), 2, 8) -- Use the correct function
Of course, it may not be this simple; so you may have to check variables as they are being passed to your functions, as well.
As a rule of thumb: math.* expects a number as the first argument, string.* expects a string as the first argument, etc.
Note: Not really sure how to explain this error;
This means that your computer isn't allowing ComputerCraft to run other computers, and update it's display, etc:
while true do
...
end
This code will cause this error, since there are no points in it that ComputerCraft can pause it, and execute other code.There are a few ways of fixing this:
The easiest one is:
while true do
sleep(0)
...
end
Adding the sleep call will allow computercraft to do other stuff, then resume your function from where the sleep call is.Another method is using os.pullEvent in a loop; but I'm not going to explain how to do that here.
This happens for a few reasons; the ones I've encountered so far are:
1) The stack overflows (<number> will be 256)
function asd()
asd()
end
asd()
This will cause the error; since you will be going deeper into the stack with every call of asd.You can also do this by using multiple functions:
function a()
b()
end
function b()
c()
end
function c()
a()
end
a()
Possible solutions are:
Tail calls:
You can read more about this here: Programming in Lua c. 6.3
function asd()
return asd()
end
Lua uses tail calls; what this means is that, if you call a function right after the return statement, the stack level will be re-used.Examples of tail calls:
return asd()
Invalid examples:
return 1, asd() -- Adding return values
local a, b = asd()
return a, b -- Not a tail call; function is called before returning
asd() -- Reducing return values to 0
Looping:
You can use a loop instead of using a function, while storing the data.
(No example yet because lazy + forum editing sux)
2) You're using string.gsub/other string.* function that uses patterns, and your [] are mismatching:
string.gsub("a]b]c]d]", "]", "!")
Possible solutions:
If you just want to match the [ or ] character, use %[ and %].
The % sign escapes non-alphanumeric characters in patterns.
If you're using [] to match a group of characters, you'll need to find out where you're missing them.
These errors are produced when the lexer finds an error, e.g:
if a = b then ... -- you missed a = or a ~; 'then' expected
end
while true do
...
end
end -- An additional end '<eof>' expected
if true then
...
-- Missing an end; 'end' expected
If your error ends with "expected", then this is most likely a syntax error.
This error is fairly easy to find, once you figure out what the error means:
You have multiple points (.) in a number!
What will cause this error?
version = 1.0.0 -- This will error! You can't have multiple points in a number!
How would we fix this?
Since you most likely would like to keep the minor version (1.0.0),
you can simply turn it into a string (which is probably what you meant to have it as):
version = "1.0.0" -- Fixed!
version = 1.0 -- If you want to keep it a a number, you’ll have to drop one of the .s
version = 1 -- You can also split it into multiple vars
version_major = 0
version_minor = 0