ITEC 1011 A,B
Week 7 Lab Instructions
Oct 26, 27 2000

Debugging Java Programs


  1. To LEARN ABOUT compile and run-time errors.
  2. To INTERPRET error messages.
  3. To CORRECT errors.

Probably the most disheartening thing that happens while programming, is seeing a long list of error messages when you first compile a new program. Unfortunately, this is a fact of life, and it happens to everyone. However, this mess of text does have meaning. The error messages produced by the java compiler are there to assist the programmer in generating correct code.

  1. Compile errors

    These are the most common type of error. In fact most of them are caused by simple typos or forgeting a ; or }. In this case, the compiler gets confused, and doesn't know what you are talking about. The end result is that your code is not compiled, and the .class file is not generated. Usually it is a simple matter of reading the error messages, and editing the code to correct the typo. In other programming languages, some of these may be called "syntax errors".

    Sometimes, you will make a mistake in the data types involved . For example, if you try to assign a string value to an integer, java will again be confused and will refuse to compile.

    Correcting these errors is not that difficult, In fact, Java usually tells you what the error is and, the line of code in which it occurs. Since DOS edit displays the current line number in the bottom right corner, it is easy enough to solve this problem. I guarantee that at one point, you will get an error "Expected ';' in line 56". This is usually solved by adding a semi-colon at the end of line 56. Pretty simple isn't it.

  2. Run Time Errors

    These are trickier. The program has compiled, and you think you're ok, but when you run the program, it crashes. Sometimes the program will print out a cryptic message like "ClassDefNotFound" and sometimes it will just output something totally unexpected. There are several things you should check when this happens.
    1. Class names are spelled properly and they match the file name in which they are found. Make sure that the files are in the directory that they are expected to be in.
    2. Numeric data types are correct for the expected values. This is particularly true with integer types. Once you reach the maximum value of an integer type, adding one will cause it to "Roll Around" to the maximum negative value. For example take the 8 bit twos complement number 0111 1111 or 127. Add one to it, and you get 1000 0000 which is -128. This would be a very significant error if these numbers were used in calculating hours worked in a month or such. Instead of getting paid, I would owe the company money!
    3. Remember that 9/2 = 4 and that 9/2.0 = 2.5. If you are ever unsure, use an explicit cast. ie (double) 9/ (double) 4 . Often this is redundant, but it doesn't hurt.
    4. Make sure you didn't accidentally comment out a line.
    5. This is by no means an exhaustive list, but it gives you a good starting point. The only way to really learn this is to make errors, and to solve them. Practice makes perfect.

  3. Notes on DOSKEY

    This is a tool that will make your life easier. If you are working in a lab at school, it is automatically activated when you run the init.bat program. It allows you to repeat past actions without re-typing them. You can do this by using the up and down arrows to scroll through your past commands. You can also edit a previous line by backspacing and typing over the text. The insert key will toggle between insert and typeover mode. Play with it for a while and you will gain experience.

  • Tasks:

    1. Download and unzip these files.
    2. There are 5 files in this package named:

      Each one contains an error. Try to compile each program and then run it.
    3. In an E-mail to or<with the subject line oct29: lastname studentnumber, write a brief description of the error in each program and what you had to do to fix it. Please try and keep your answers as brief as possible while still being complete.

      A sample format for most of your answers is

           The error was in line * .
           The problem was **************.
           It was fixed by **************.


    You will not be marked on what follows. This is just suggested (highly recommended) work to help you understand java better. I will be pleased to answer any questions on this.
    1. Compile and run all of the java programs in chapters 2,3,4 of the notes. Take a look at the code listings and try and understand how the program works.
    2. Don't be afraid to try and modify any of these programs. In fact, it is encouraged. Try adding another modifying (2-27) to perform the conversion in the opposite direction (ie input is centimeters, output in Inches.) Now modify it to give output in feet and inches. (Copy the file first so you don't destroy the original)
    3. Modify so that it uses a switch structure rather than an if-then-else structure. If you can do this, then you have a good understanding of the material.