Here's a draft of a "common errors" section for the sticky post revamping. I'd like feedback on whether the phrasing of any of these is confusing, and suggestions for any other common error messages that we see a lot, if any.
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 exp
ression 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 throw 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.
And here's a draft of advice to users posting questions.
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
de] before your code and [/code] 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.
Edited by Lyqyd, 03 August 2013 - 08:43 PM.
changed phrasing.