Contents |
This section covers some common problems that you might encounter when learning the Java language. After each problem is a list of possible solutions.Problem: The compiler complains that it can't find a class.
- Make sure you've imported the class or its package.
- Unset the
CLASSPATH
environment variable, if it's set.- Make sure you're spelling the class name exactly the same way as it is declared. Case matters!
- If your classes are in packages, make sure that they appear in the correct subdirectory as outlined in Managing Source and Class Files.
- Also, some programmers use different names for the class name from the
.java
filename. Make sure you're using the class name and not the filename. In fact, make the names the same and you won't run into this problem for this reason.Problem: The interpreter says it can't find one of my classes.
- Make sure you specified the class name--not the class file name--to the interpreter.
- Unset the
CLASSPATH
environment variable, if it's set.- If your classes are in packages, make sure that they appear in the correct subdirectory as outlined in Managing Source and Class Files.
- Make sure you're invoking the interpreter from the directory in which the
.class
file is located.Problem: My program doesn't work! What's wrong with it?
The following is a list of common programming mistakes by novice Java programmers. Make sure one of these isn't what's holding you back.
- Did you forget to use
break
after eachcase
statement in aswitch
statement?- Did you use the assignment operator
=
when you really wanted to use the comparison operator==
?- Are the termination conditions on your loops correct? Make sure you're not terminating loops one iteration too early or too late. That is, make sure you are using
<
or<=
and>
or>=
as appropriate for your situation.- Remember that array indices begin at 0, so iterating over an array looks like this:
for (int i = 0; i < array.length; i++) . . .- Are you comparing floating-point numbers using
==
? Remember that floats are approximations of the real thing. The greater than and less than (>
and<
) operators are more appropriate when conditional logic is performed on floating-point numbers.- Are you having trouble with encapsulation, inheritance, or other object-oriented programming and design concepts? Review the information in Object-Oriented Programming Concepts.
- Make sure that blocks of statements are enclosed in curly brackets
{ }
. The following code looks right because of indentation, but it doesn't do what the indents imply because the brackets are missing:for (int i = 0; i < arrayOfInts.length; i++) arrayOfInts[i] = i; System.out.println("[i] = " + arrayOfInts[i]);- Are you using the correct conditional operator? Make sure you understand
&&
and||
and are using them appropriately.- Do you use the negation operator ! a lot? Try to express conditions without it. Doing so is less confusing and error-prone.
- Are you using a
do
-while
? If so, do you know that the loop executes one more time than a similarwhile
loop?- Are you trying to change the value of an argument from a method? Arguments in Java are passed by value and can't be changed in a method.
- Did you inadvertently add an extra semicolon (
;
), thereby terminating a statement prematurely? Notice the extra semicolon at the end of this for statement:for (int i = 0; i < arrayOfInts.length; i++) ; arrayOfInts[i] = i;
Contents |