Create Software Components 

Using Java Level 2

 

 

Course Info

Scheme

Resources

Tutorials

Java Demos

Utilities

Links


 

   

Debugging Tips

You will frequently be faced with inexplicable messages from the Java compiler and interpreter.  Don't pull your hair out, you gradually get to know what your compiler really means when it says weird things like "{ expected."

The sections below deal with;-

Dealing with Error Messages - techniques for finding the source of the problem

Compiler Error Messages - error messages you get when trying to compile source code to bytecode using the javac tool

Interpreter Errors Messages - error messages you get when trying to run your class code using the java tool.


Dealing with Error Messages

Sometimes one tiny little syntax error creates an avalanche of incomprehensible compiler error messages. Luckily, Java gives the source code line number along with any error message.  This means you must find the offending lines in your code and examine them for syntax errors.

First Rule: Examine the first error message and deal with that first. Don't try to figure out all the messages at once.  Think about any changes you have just made to your code..  Fix any errors and recompile.

Second Rule: If the error message does not make sense and you think the line that Javac is complaining about is perfect, then examine a few of the lines before that line for syntax errors. Fix any errors and recompile.

Third Rule:  On recompiling you may find other errors to deal with.

Most Important Rule Ever.  Every time you make a few changes to your code, COMPILE YOUR CODE to check for syntax errors. Unless you are a Java guru and can sit there happily typing in lots of error free code you WILL make syntax errors.  The more you make before compiling, the harder you will find it interpreting the heaps of complaints you are likely to get from the compiler.


Here are some simple techniques you should use when your programs don't work.

 

    Use Comments

 If you can't find a problem...

comment out single lines:-

// this line will not be executed.

comment out sections of code:-  that include the problem area:

/*
none of these lines

will be executed

*/

You can keep reducing or increasing the number of lines commented out until the problem line is found.

 

    Use System.out.println

System.out.println() is very useful for...

finding out the values of variables as your program is running. 

informing you that a particular line in your code has been reached.  This is especially good for determining which choice was made in selection statements (if-else statements and switch).

when testing a GUI or Applet, the System.out.println()is handy because the output is sent to the command console and so it does not interfere with the running of your program. 

 


Compiler Error Messages

If you see any compiler errors, then your program did not successfully compile, and the compiler did not create a .class file. Carefully verify your source code, fix any errors that you detect, and try again. Below are some common problems.

 

Can't Locate the Compiler

You may see the following error message if your path isn't set properly:

javac: Command not found

Modify your PATH environment variable so that it includes the directory where the Java compiler lives. Refer to Modifying Your Path

 

';' expected

If you mistype part of a program, the compiler may issue a syntax error. The message usually displays the type of the error, the line number where the error was detected, the code on that line, and the position of the error within the code. Here's an error caused by omitting a semicolon (;) at the end of a statement:

HelloWorld.java:10: `;' expected.
System.out.println("Hello World!")
                                  ^
1 error

If you get this type of error message, then check your source code and correct it.

'{' expected

Here we have another syntax error. Usually it means you put a semicolon where one was not supposed to be ahead of a {  

Or you have omitted a  {  

Check your source code and correct it.

 

 


Interpreter Error Messages

You may get error messages when trying to run your code either using the java utility or the appletviewer/browser

 

Can't Find the Class

A common error of beginner Java programmers  is to try to interpret the .class file created by the compiler. 

For example, if you try to interpret the file HelloWorld.class by typing

java HelloWorld.class

the interpreter displays this error message:

Can't find class HelloWorld.class

The argument to the Java interpreter is the name of the class that you want to use, not the filename. So in this case what should be typed in is:-

java HelloWorld

Exception in thread "main" 

There are a few different possible causes of the error message:-

Exception in thread "main" java.lang.NoClassDefFoundError: FileName

You may have forgotten to compile your .java source code into a .class file

e.g.. Here I have a source code file SoundExample.java but I have forgotten to compile it to a class file. Typing in:-

java SoundExample

gives the error message 

Exception in thread "main" java.lang.NoClassDefFoundError: SoundExample

 

Exception in thread "main" 

Possibly the class name was incorrectly typed.

e.g.. Here I have a file SoundDocument.class with the class declared as SoundDocument.  Typing the following:-  

java sounddocument

gives an error

Exception in thread "main" java.lang.NoClassDefFoundError: sounddocument

Typing the following corrects the error

java SoundDocument

 

 


That it for the debugging tips folks!

 

 

  Site Home 

Java Home   

  Forum  

Course Info

Welcome

Overview

Assessment

Qualification

Scheme of Work

Assignments

Resources

Information

Blackboard

Learning Center

Web Materials

Reading

Java Demos

Utilities

Links

Lecture Materials

Tutorials & Notes

Exercises

Activities

Quizzes

 

Site Home

Top

Unit Home

ADR 2002