Posted 03 August 2013 - 04:40 PM
Table of Contents:
Many common errors can be relatively easily solved yourself, if you know what to look for. Before you post, take a look through this handy guide of several error messages we commonly see posted. If your error message is found here, try out the advice listed with it before posting a question; you might just find your answer!
attempt to index ? (a nil value)
This means that you've tried to access a key of a table that doesn't exist. If you're not working directly with tables, check the API call at the line number the error specifies (like `turtle.forward()` and such) to be sure that you haven't misspelled the name of the API. If for instance, you had `trutle.forward()` or `Turtle.forward()`, those would both result in this error.
attempt to call nil
This means that you are trying to use a function that doesn't exist! Check the line number the error specifies to be sure you've spelled the function name correctly, and check through the documentation to make sure that the function you're trying to use exists in your version of ComputerCraft.
'then' expected
Look at the line the error is on and the line above it. This error usually means that your conditional expression for your if statement is malformed (or that you're actually missing your `then`). Make sure you're using the comparison operator (==) instead of the assignment operator (=) if you're checking for equality.
'=' expected
When the interpreter throws this error, it means that it was expecting you to reassign a variable to a new value. Check the line number of the error and the line above it, and look for function calls that are missing their parentheses. Be sure to carefully read the code to ensure that you aren't misspelling keywords (like elesif instead of elseif).
'end' expected
An "end expected" error means that your code has more block-starting statements than end statements. Many statements start blocks (if, for, while, repeat, do, function), and each time you use a block-starting statement, you must end it. Each of those (except repeat, which must be ended by an `until`) should be ended by an `end` statement. Each if statement needs only one end, regardless of how many elseif statements it also contains.
'eof' expected
This error is the opposite of the previous error. Instead of too few block-ending statements, you have too many. The interpreter throws this error when it reaches an `end` (or an `until`) that doesn't close a block you've previously opened. Verify that each of your ends and untils match their opening statements.
too long without yielding
Yield protection is a ComputerCraft-specific issue, due to the nature of the Java side handling of the Lua environment for the computers. This message means that your program has run for ten seconds without yielding, which usually means you have an infinite loop that is constantly running. You can fix this simply by adding a sleep(0), but it's better to determine how often your code actually needs to run and use an os.pullEvent() to wait for the events it needs. For instance, a loop which looks at changes in redstone state could include an os.pullEvent("redstone") to wait for changes in redstone state before iterating through the loop again.
java.lang.ArrayIndexOutOfBoundsException: 256
This is frequently caused by using a function that calls itself to create an infinite loop. You should instead use an actual loop structure. Most commonly, for infinite loops, you would use `while true do` to begin the loop and `end` to end it. To exit the loop, you could use the `break` keyword. If you actually need to use recursive functions, you can use proper tail-calls where possible to avoid overflowing the stack. However, if all you need is an infinite loop, it is preferable to use an actual loop instead. Here is a simplified example of creating an infinite loop with recursion, and the same function with a proper tail-call:
Multiple points
This error is thrown when the Lua interpreter thinks you've tried to create a number with multiple decimal points in it (such as 1.0.0). This error tends to crop up when concatenating numbers into strings. If you need a number with multiple points for some reason (such as version numbering), you'll want to use a string.
<a id="tutorials">Ask a Pro Tutorial Series</a>
The Ask a Pro Tutorial Series is a group of tutorials created by Ask a Pro contributors specifically to help introduce new members to ComputerCraft, from the most basic concepts to some more advanced examples and code.
When creating a question topic, be sure to fully explain your question. If you're having a problem with a piece of code, you should explain three things about it. Explain what you're trying to do, the big picture goal for the program. You won't need a huge amount of detail for this necessarily, but it helps to know where you intend to take the project. Next, explain what you expect the piece of code that you're having trouble with to do. Finally, explain what the code is actually doing instead of what you want it to do. This will help us get started on answering your question effectively. Be sure to include the latest copy of your code, and the full error message text if you receive an error while running it. It is very important that we see your code and the full error message (if any) so that we can help you diagnose and correct your code as quickly as possible.
If your code isn't too long (around 200 lines or less), it's okay to post it in the forums in code tags. Place [co<b></b>de] before your code and after. Make sure that there is no formatting on the text of the code when you post it, as the code block will format your code for you. If your code is too long, it is suggested that you post it to pastebin and provide a link to the paste in your post. If you cannot copy and paste the code (for instance, it is on a server and you cannot access the files), and you have to copy it out by retyping it, please be extra certain to verify that every character matches exactly in the code. It is very frustrating for both the person seeking help and those trying to help if we are trying to debug code that you're not actually using.
Over the course of your question topic, you may require additional help after your initial problem is solved. Each time you need to ask a further question, please re-post the code into your reply in that topic if it has changed since the last time you posted it. We cannot guess the exact changes you've made, and so it is necessary to see the updated code. You should use a single topic for all of your questions on one piece of code, as it is easiest for the answer posters to see the history of the code all in one place, and it prevents multiple people from providing the same advice in two different topics if they see subsequent problems in your code.
When choosing a topic title, make sure to use something descriptive of your question. For instance, "lua error" is a bad topic title, but '[string "woodcutter"]:13: attempt to index ? (a nil value)' is a good topic title. It is always advisable to use the full text of your error message if you are receiving one (and you cannot solve it by consulting the "common errors" guide). The first one ("lua error") isn't very descriptive. Of course you have a Lua question! The second title is much better, as it gives answer posters an idea of the problem before they even open the topic. If your question is more general, use an appropriately descriptive topic title that gets to the center of your question, like "How do I retrieve data from a broken computer?", or "What do I pass to parallel.waitForAll?"
Some questions, as alluded to above, aren't about a specific piece of code at all, but rather are more general in nature. For these sorts of questions, it's still helpful to give a lot of information about what you're trying to do or why you're needing the information. The more information you give, the better we can answer your question, and point you to additional resources that may help you on your way.
<a id="faster_help">Get Help Faster</a>
Generally, both in Ask a Pro and elsewhere, the amount of effort people are willing to put in to answer a question is dependent on how much effort it looks like the asker has put in to forming the question. This is for two reasons; people are more willing to help someone who seems to have put in effort trying to find the answer themselves, and it is easier to find a solution to a well-formed question than to find an answer to a poorly-formed question.
One major thing you can do to improve the quality of your question is to ensure that your code is well-formatted. Indentation especially will help, as it makes your code vastly more readable and much easier to follow. It is also easier to spot mismatched code blocks (extra `end` statements, or too few of them) and other problems. Here is an example of how to indent:
As we read through the code, we add a level of indentation to the lines following the opening of a block, and remove a level of indentation for each line that closes a block. Peer-level statements (like else and elseif) are on the same level of indentation as the statement that opened the block they are in. It sounds more complicated than it is. Here is a break-down of the process.
function begins a block, so we indent the lines after it.
if begins a new block, so we add a layer of indentation. else is on the same level as the if that it is part of, so it should be indented only as far as the if statement is. The end statement that closes the if drops us back a level, so it ends up on the same level of indentation as the opening statement of the block.
We continue indenting and find the closing end of the function, so for this example, we are done. Indenting is an easy way to increase readability and make debugging easier. Indentation is something that is easy to do as you code, and many text editors can help you automatically indent your code as you go, and some even include tools to indent code
In addition to well-formatted code, a description of your program and what it does or is supposed to do can be helpful for figuring out the intent behind the code. A good explanation of what the code currently does is very useful along with a description of what you expected it to do instead, or what you'd like it to do. This information also is very helpful for us to be able to provide you with the best answers we can. Also, see the tips about choosing a good topic title in the previous section.
- <a href="#common_errors">Common Errors</a>
- <a href="#tutorials">Ask a Pro Tutorial Series</a>
- <a href="#good_question">Posting a Good Question</a>
- <a href="#faster_help">Get Help Faster</a>
Many common errors can be relatively easily solved yourself, if you know what to look for. Before you post, take a look through this handy guide of several error messages we commonly see posted. If your error message is found here, try out the advice listed with it before posting a question; you might just find your answer!
attempt to index ? (a nil value)
This means that you've tried to access a key of a table that doesn't exist. If you're not working directly with tables, check the API call at the line number the error specifies (like `turtle.forward()` and such) to be sure that you haven't misspelled the name of the API. If for instance, you had `trutle.forward()` or `Turtle.forward()`, those would both result in this error.
attempt to call nil
This means that you are trying to use a function that doesn't exist! Check the line number the error specifies to be sure you've spelled the function name correctly, and check through the documentation to make sure that the function you're trying to use exists in your version of ComputerCraft.
'then' expected
Look at the line the error is on and the line above it. This error usually means that your conditional expression for your if statement is malformed (or that you're actually missing your `then`). Make sure you're using the comparison operator (==) instead of the assignment operator (=) if you're checking for equality.
'=' expected
When the interpreter throws this error, it means that it was expecting you to reassign a variable to a new value. Check the line number of the error and the line above it, and look for function calls that are missing their parentheses. Be sure to carefully read the code to ensure that you aren't misspelling keywords (like elesif instead of elseif).
'end' expected
An "end expected" error means that your code has more block-starting statements than end statements. Many statements start blocks (if, for, while, repeat, do, function), and each time you use a block-starting statement, you must end it. Each of those (except repeat, which must be ended by an `until`) should be ended by an `end` statement. Each if statement needs only one end, regardless of how many elseif statements it also contains.
'eof' expected
This error is the opposite of the previous error. Instead of too few block-ending statements, you have too many. The interpreter throws this error when it reaches an `end` (or an `until`) that doesn't close a block you've previously opened. Verify that each of your ends and untils match their opening statements.
too long without yielding
Yield protection is a ComputerCraft-specific issue, due to the nature of the Java side handling of the Lua environment for the computers. This message means that your program has run for ten seconds without yielding, which usually means you have an infinite loop that is constantly running. You can fix this simply by adding a sleep(0), but it's better to determine how often your code actually needs to run and use an os.pullEvent() to wait for the events it needs. For instance, a loop which looks at changes in redstone state could include an os.pullEvent("redstone") to wait for changes in redstone state before iterating through the loop again.
java.lang.ArrayIndexOutOfBoundsException: 256
This is frequently caused by using a function that calls itself to create an infinite loop. You should instead use an actual loop structure. Most commonly, for infinite loops, you would use `while true do` to begin the loop and `end` to end it. To exit the loop, you could use the `break` keyword. If you actually need to use recursive functions, you can use proper tail-calls where possible to avoid overflowing the stack. However, if all you need is an infinite loop, it is preferable to use an actual loop instead. Here is a simplified example of creating an infinite loop with recursion, and the same function with a proper tail-call:
--this function will throw the error
function printMany()
print("text")
sleep(0)
printMany()
end
--this function will not
function printMany()
print("text")
sleep(0)
return printMany()
end
--but you should do this instead, wherever you can.
function printMany()
while true do
print("text")
sleep(0)
end
end
Multiple points
This error is thrown when the Lua interpreter thinks you've tried to create a number with multiple decimal points in it (such as 1.0.0). This error tends to crop up when concatenating numbers into strings. If you need a number with multiple points for some reason (such as version numbering), you'll want to use a string.
<a id="tutorials">Ask a Pro Tutorial Series</a>
The Ask a Pro Tutorial Series is a group of tutorials created by Ask a Pro contributors specifically to help introduce new members to ComputerCraft, from the most basic concepts to some more advanced examples and code.
- Computer Basics
- I: Crafting, using the shell and lua prompt, cd, list and edit.
- II: Help, programs, label, id, copy, move, rename and mkdir.
- III: Crafting disk drives/floppies, use of drives, labeling disks, dying disks.
- Peripheral Basics
- Turtles
- Basics
- Intermediate
- Event Basics
- API
- Basics: Purpose for using, functions in APIs, simple example API.
- Intermediate: Data in APIs, when appropriate, use and effect of local in APIs.
- Advanced: Datatype APIs.
When creating a question topic, be sure to fully explain your question. If you're having a problem with a piece of code, you should explain three things about it. Explain what you're trying to do, the big picture goal for the program. You won't need a huge amount of detail for this necessarily, but it helps to know where you intend to take the project. Next, explain what you expect the piece of code that you're having trouble with to do. Finally, explain what the code is actually doing instead of what you want it to do. This will help us get started on answering your question effectively. Be sure to include the latest copy of your code, and the full error message text if you receive an error while running it. It is very important that we see your code and the full error message (if any) so that we can help you diagnose and correct your code as quickly as possible.
If your code isn't too long (around 200 lines or less), it's okay to post it in the forums in code tags. Place [co<b></b>de] before your code and after. Make sure that there is no formatting on the text of the code when you post it, as the code block will format your code for you. If your code is too long, it is suggested that you post it to pastebin and provide a link to the paste in your post. If you cannot copy and paste the code (for instance, it is on a server and you cannot access the files), and you have to copy it out by retyping it, please be extra certain to verify that every character matches exactly in the code. It is very frustrating for both the person seeking help and those trying to help if we are trying to debug code that you're not actually using.
Over the course of your question topic, you may require additional help after your initial problem is solved. Each time you need to ask a further question, please re-post the code into your reply in that topic if it has changed since the last time you posted it. We cannot guess the exact changes you've made, and so it is necessary to see the updated code. You should use a single topic for all of your questions on one piece of code, as it is easiest for the answer posters to see the history of the code all in one place, and it prevents multiple people from providing the same advice in two different topics if they see subsequent problems in your code.
When choosing a topic title, make sure to use something descriptive of your question. For instance, "lua error" is a bad topic title, but '[string "woodcutter"]:13: attempt to index ? (a nil value)' is a good topic title. It is always advisable to use the full text of your error message if you are receiving one (and you cannot solve it by consulting the "common errors" guide). The first one ("lua error") isn't very descriptive. Of course you have a Lua question! The second title is much better, as it gives answer posters an idea of the problem before they even open the topic. If your question is more general, use an appropriately descriptive topic title that gets to the center of your question, like "How do I retrieve data from a broken computer?", or "What do I pass to parallel.waitForAll?"
Some questions, as alluded to above, aren't about a specific piece of code at all, but rather are more general in nature. For these sorts of questions, it's still helpful to give a lot of information about what you're trying to do or why you're needing the information. The more information you give, the better we can answer your question, and point you to additional resources that may help you on your way.
<a id="faster_help">Get Help Faster</a>
Generally, both in Ask a Pro and elsewhere, the amount of effort people are willing to put in to answer a question is dependent on how much effort it looks like the asker has put in to forming the question. This is for two reasons; people are more willing to help someone who seems to have put in effort trying to find the answer themselves, and it is easier to find a solution to a well-formed question than to find an answer to a poorly-formed question.
One major thing you can do to improve the quality of your question is to ensure that your code is well-formatted. Indentation especially will help, as it makes your code vastly more readable and much easier to follow. It is also easier to spot mismatched code blocks (extra `end` statements, or too few of them) and other problems. Here is an example of how to indent:
Spoiler
We start out with a block of code that has no indentation.
function doThing(stuff)
local returnValue
if stuff == 1 then
returnValue = 2
else
returnValue = 3
end
return returnValue
end
As we read through the code, we add a level of indentation to the lines following the opening of a block, and remove a level of indentation for each line that closes a block. Peer-level statements (like else and elseif) are on the same level of indentation as the statement that opened the block they are in. It sounds more complicated than it is. Here is a break-down of the process.
function doThing(stuff)
local returnValue
if stuff == 1 then
returnValue = 2
else
returnValue = 3
end
return returnValue
end
function begins a block, so we indent the lines after it.
function doThing(stuff)
local returnValue
if stuff == 1 then
returnValue = 2
else
returnValue = 3
end
return returnValue
end
if begins a new block, so we add a layer of indentation. else is on the same level as the if that it is part of, so it should be indented only as far as the if statement is. The end statement that closes the if drops us back a level, so it ends up on the same level of indentation as the opening statement of the block.
function doThing(stuff)
local returnValue
if stuff == 1 then
returnValue = 2
else
returnValue = 3
end
return returnValue
end
We continue indenting and find the closing end of the function, so for this example, we are done. Indenting is an easy way to increase readability and make debugging easier. Indentation is something that is easy to do as you code, and many text editors can help you automatically indent your code as you go, and some even include tools to indent code
In addition to well-formatted code, a description of your program and what it does or is supposed to do can be helpful for figuring out the intent behind the code. A good explanation of what the code currently does is very useful along with a description of what you expected it to do instead, or what you'd like it to do. This information also is very helpful for us to be able to provide you with the best answers we can. Also, see the tips about choosing a good topic title in the previous section.