This is not meant to be a comprehensive debugging tutorial. Instead it is meant to help the new coding student understand the error Visual Studio is reporting so they can figure out for themselves what is cause and fix their own code. I would file it under "Give a man a fish and he eats for a day. Teach a man to fish and he eats for a lifetime."
It is still very important to work through the excellent debugging tutorials here. One or two hours spent on these tutorials will save you 100 hours on your first project. Not to mention help save your attitude and sanity. For the life of me I can't understand why debugging isn't taught as part of your university course. But judging by the questions we get here, it isn't. If you take the time to get good at it you will be head and shoulders above your class-mates.
Spoiler
Locate the error
First there should be no mystery about where the error is occurring. 99% of the time Visual Studio halts execution when the error is hit and highlights the line. At this point you can hover the mouse over any of your variables and the value will be displayed in the tooltip.

You should have the Autos and Locals pallets open. These let you see the values of all the variables in the current scope. So often just seeing the 4 important variables you are working with will guide you to the problem: All it takes is to see one of them at 0 when you were expecting 143 to point you to the problem.

Null reference... Value cannot be null...
Now that you know how to see the values of the variables this one should be easy. Just move your mouse from one variable to the next pausing briefly for the tooltip to update with the value, until you see which one is null.
Object {name} is not set to the instance of an object.
Take this example:
Button demo; Demo.Text = "failure";
demo was declare but it was never instantiated, meaning it was never set to the instance of something. See the correction here?
Button demo = new Button; Demo.Text = "success";
Stack overflow
This is almost always the result of an infinite loop of method/function calls. The list of methods called is stored on the Stack. This is so when a method is complete and can return, it knows where to return. But the Stack is of limited size. So if you have this situation:
void methodOne()
{
methodTwo();
}
void methodTwo()
{
methodOne();
}
You have a never ending circle of one calling two calling one calling two calling one calling two. Eventually overflowing the Stack: Thus "Stack overflow"
Cannot apply mathematical operation to string
"5" + "5" = "55" not "10"
You probably used a Textbox to get a numerical value. Textbox values are strings not numbers. So you either need to convert the string to a number before doing the math, or better yet use a GUI control meant for just numbers like a NumericUpDown or a NumericTextBox. If you choose to try to parse text to a number don't forget that it could fail and you need to code for that eventuality. When a user types "five" or "yogi bear" in a Textbox you have to handle it gracefully.
The object {name} does not exist in the given context.
This is all about scope or the lifetime of an object/variable. In the simplest terms a thing exists only within the code block it was defined in. As long as that block remains alive so does your thing and your ability to reference it. In this example just because we made a picnic.basket doesn't mean our Ranger knows anything about it because he wasn't part of that class. Its sad when you aren't invited to the picnic.
namespace jellystone
{
public class picnic
{
public Object basket; // This exists only within the picnic
public NumberOfSandwiches = 6;
void someMethod()
{
int x;
}
void anotherMethod()
{
int x; // NOT the same x as in someMethod()
}
}
class Ranger
{
void different method()
int count = basket.NumberOfSandwiches; // basket is unknown here. Undefined in the context of this method of this class
}
}
Not all code paths return a value
You have some different paths of execution for your method, maybe through a series of if checks or switch statements. Your method is defined to return a value but one or more of those possible paths have no return.
string HiMediumOrLow(int someNumber)
{
if (someNumber < 100) return "low";
else if (someNumber > 500) return "high";
}
What happens if someNumber is 250? There is no path of execution that will return a value such as "medium". By adding a final return that is hit if no other condition is true we know we will always have something return.
string HiMediumOrLow(int someNumber)
{
if (someNumber < 100) return "low";
else if (someNumber > 500) return "high";
return "medium";
}
Index was outside the bounds of the array/collection
So you have some array or collection. First realize that collects are zero-indexed meaning the first element is [0] The second is [1] The third is [2] The fourth is [3] ...
So if you have an array of 10 items they are elements 0-9. Element [10] would be the eleventh item out 10. 11 is outside the limits {bounds} of the collection. You need to make sure your collection has as many things as you are trying to access.
This includes strings because they are collections of characters. If you are trying to chop up a string input by the user you can't assume they gave you a long enough string to work with. If you try to get 10 characters starting from the 5th, you have to have at least 15 characters. If the user entered 12 and you don't check before reaching past the end of the string then you are reaching beyond the bounds of the array {of characters}.
Out of memory error - when trying to open a jpg
This error lies! You aren't really out of memory. It's just that the image is either still being written by another process or it is corrupt. If you are reacting to a FileSystemWatcher it is probably still being written. The FSW raises its new item event when a new entry is written in the file allocation table of the drive, not when the new file is done being written. Try adding a loop so you keep trying for 20 seconds. If it still fails the file might be corrupt. This can cause the GUI to look unresponsive if done on the same thread so loading files is best done on a new thread.






MultiQuote











|