240 likes | 398 Views
Debugging. Debugging Review. Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime . – Ancient Proverb
E N D
Debugging Review • Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for a lifetime. – Ancient Proverb • Help a student to fix a bug, and he or she is set until the next bug. Teach that student to use the debugging tools, and maybe we can all get some sleep. – IOE 373 Proverb
Debugging Hint 1: Read the Error Message • Not all error messages are easy to understand, but some are. • I’m always amazed at the forum posts which say “I ran this code and got an error.” • Um…What did the error say??? • By now, you should know what “Object reference not set to an instance of an object” means (if not, wait a couple of slides). • But many other error messages are pretty straightforward, or should be to you by now. • It’s frustrating when your code doesn’t work, but let VB help you to solve the problem by reading the message, and then remember debugging hint 2…
Debugging Hint 2: Don’t Hit the Stop Button! • If you run your code and you get to a point where it stops on a line of code, read the message, and then do… • Nothing! When VB stops on a line of code, it is in debugging mode. • By stopping on that line of code, VB is giving you a big hint: THERE’S SOMETHING WRONG WITH THAT LINE OF CODE! • If you stop debugging, you will lose access to LOTS of information that can help you to solve the problem.
In this example, we have experienced the most common error: “Object reference not set to an instance of an object.” • VB doesn’t tell us which reference is not set—we need to find that out.
Don’t Stop That Program! • This information is pretty easy to find—if we don’t stop the program. • We are looking for a variable with a value of “Nothing”. • We can use Quick Watch to find it. Recall that Quick Watch is available on the right-click menu; right click on the variable that you want to check, and select Quick Watch.
QuickWatch on s indicates that it is not Nothing: It contains an actual Student object.
However, a QuickWatch on the AllStudents List reveals that it is Nothing: We never created the List object by calling New.
Fixing the Bug • NOW, we can stop debugging. We know the cause of the error: We are trying to add items to a List object that doesn’t exist yet. • To fix the problem, we need to create the List before using it. • Assuming that we will want to use that List elsewhere in the form’s code, the best places to create it would be when the form is created. Either: • The Form Load event, or • In the constructor, if we have written a custom constructor for this form.
Form Load • Here is the form’s code using Form Load to create the List:
Constructor • Here is the form’s code using a custom constructor to create the List:
No Error, No Action? • What if you run your program, and it seems to freeze? • That is, you do not get an error message, but the program hasn’t done what it was supposed to do, and it doesn’t respond to mouse clicks? • It is probably stuck in an infinite loop.
Debugging an Infinite Loop • If this happens, the first thing to do is to switch to Visual Studio and click on the “Break All” button. (Pause)
Break All • Break All forces the program to pause execution; VB will highlight the line of code currently running. • You can then step through the code to see what’s happening. • In this case, the value of i never changes, so it is never greater than 100. • The Watch Window can help you to see what’s going on. (Next slide)
In this case, the program has added 23027 zeros to the ListBox, and i is still zero. • Whenever you use Do loops, make sure that the exit condition will eventually be met. • For loops generally do not have this problem.
Other possible causes of infinite loops • Aside from Do Loops, there are other ways to end up in an infinite loop: • Recursive subs/functions: These are subs or functions that call themselves. These can be very useful, but they must always contain a way out, so that eventually they stop calling themselves. • Calling back and forth. If Sub A calls Sub B, and Sub B calls Sub A, you can easily get caught in an infinite loop. • In both of these cases, or any case where the program seems to be stuck, hit the “Break All” button. • If the Call Stack is gigantic, you probably have one of these problems.
Startup Errors • Some of the most annoying errors are runtime errors where the program crashes without stopping on a line of code. • These are very hard to find, since VB gives you very few clues. • In most cases, I have found these errors to be caused by writing executable code outside of any procedure. • In particular, the problem frequenty comes from calling constructors (New) outside of any sub or function. • If your program fails on startup, check your code for places where you have any code besides declarations (Dim, Public, Private) outside of procedures. • See the next slide for examples.
Executable Code in Subs • Instead of this, • Do this:
When all else fails • Every semester, four or five students have something go very wrong in their programs. • They were just about finished, most things were working, and they try to add one last feature. All of a sudden… • Nothing works! • Frequently, what has happened is that something has become corrupted in the project file.
What to do • The solution to this situation (we’ll call it plan B) usually takes about 15 minutes, so • Don’t spend more than 15 minutes or so trying to figure out what went wrong before resorting to plan B. • Plan B involves creating a new VB project and copying your forms and code into this new project.
Plan B, Step by Step • Save everything you have, preferably in two places. Make sure that you know the path to the directory that contains your forms and code. • Open Visual Studio and create a new VB project. Give it an appropriate name (Assignment6_PlanB, for example). • In the Solution Explorer, delete Form1 from the project. • Without doing anything else, save this new project in a directory. • Note the directory which now contains the new project file (Assignment6_PlanB.vbproj).
Plan B, Step by Step, continued • Copy all of the code (*.vb), resource (*.resx), and designer (*.Designer.vb) files from your old project directory to your PlanB project directory. (Don’t copy the vbproj, sln, or suo files.) • If you closed your PlanB project, reopen it in Visual Studio. If you had a Form1 in your original project, you should exclude • From the Project menu, select “Add Existing Item”. • Select the forms and classes that you copied in from your original projects. • Note that for forms, you only need to select the vb file; Visual Studio knows to add the Designer.vb and resx files along.
Plan B, Step by Step, continued • In the project properties, Application tab, select the StartUp form that you want. • Try it out. If it works, save this new project and work with it from now on. • If it doesn’t work, there may be a problem with one individual form or class. • Plan C is to individually add or remove forms and classes to see which one is causing the problem. • If you isolate the problem form/class, you may be able to recreate it by starting with a new form or class in this new project, and then copying the controls and code from the matching form/class in the old project.
Plan B Summary • Resorting to plan B is a pain, but • It usually works, and • Spending 15 minutes on plan B is better than spending hours staring at a project that isn’t working.