Department of Physics & Astronomy

An Introduction to
Computational Methods in Physics

What to do when the code doesn't work

Very, very few codes are written which work first time. Whenever you type

javac Mycode.java

you should expect the sinking feeling associated with a stream of error messages. These messages will direct you to the line where the compiler thinks the problem is. Your first move should be to check that line.

If your code produces hundreds of errors on compilation, it probably means the compiler has lost track of where lines begin and end. Concentrate on fixing the first error, and often many of the others will go away.

Often, you will be starting with a working code which does something like what you want, and modifying it until it does exactly what you want. As your modifications progress, you should keep a working copy of an old version to which you can return if all else fails.

Problems with brackets and semicolons are particularly hard to track. The emacs editor recognises .java files. When you type a } it will tell you which { it matches.

Dont divide integers! 1/2 = 0.

Remember java arrays start at zero. (Count to three in English "one two three", French "un deux trois", Javan "zero, one, two"). No, I dont understand it either.

Variables only exist inside the curly bracket wherein they are defined.

If you your code produces errors while running, and you dont know where it fails, it may be useful to add code to print out messages when it reaches various points. There is a more elegant way to do this by throwing exceptions. (This is not an essential part of Compmeth.)

NaN stands for "Not a number". typically, this means you've divided by zero, taken the square root of a negative number, the inverse cosine of something greater than one or somesuch.

If any object has a method called .toString , then that method is called by objectInstance.toString (of course) and when ever that method is cast to a String i.e. System.out.println(objectInstance), the toString method is invoked automatically, just like System.out.println(objectInstance.toString). So if you have a method create a string representing the object, call it toString.

Integration routines, such as Verlet, depend on a timestep . This should be considerably smaller than any typical time of the real system. You should always make an estimate of typical oscillation/rotation frequencies. Remember this is a physics course: always make sure you understand what sort of results and orders of magnitudes to expect.

Everything runs well, but the output file doesn't appear. This may happen if your disk quota is full: use quota -v to check. If it is, then you'll have to delete some files to make space. A common culprit are files called core and the cache associated with netscape or mozilla.

A common runtime error is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException at File.main(Mycode.java:69)
This means you are trying to write to the Nth member of an array which has only M members, M being less than N. Check your array dimensions, paying particular attention to line 69.

Another is
Exception in thread "main" java.lang.NoClassDefFoundError: sillyName
This means you tried to run a class from a file called sillyName.class , but there's no such class

Another is
Exception in thread "main" java.lang.NoSuchMethodError: sillyName
This means you tried to run a class from a file called sillyName.class which doesn't contain a main method

Warning: Cannot convert string "-monotype-arial-regular-r-normal--*-140-*-*-p-*-iso8859-1" to type FontStruct . Means your terminal/computer doesnt have the font that the programs wants. Typically this doesn't matter, the machine will substitute somthing sensible, but the letters on your display might look a bit odd. Often there's nothing you can do to fix this error (but it shouldn't happen in CPlab)

Other runtime exceptions are here