General Programming Problems

Problem: The compiler complains that it can't find a class.

  • Make sure that you've imported the class or its package.
  • If the CLASSPATH environment variable is set, unset it.
  • Make sure that 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 (page 238).
  • Also, some programmers use different names for the class name and the .java filename. Make sure that 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.

Problem: The interpreter says that it can't find one of my classes.

  • Make sure that you specified the class namenot the class file nameto the interpreter.
  • If the CLASSPATH environment variable is set, unset it.
  • If your classes are in packages, make sure that they appear in the correct subdirectory, as outlined in the section Managing Source and Class Files (page 238).
  • Make sure that 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 novice Java programmers make. Confirm that one of these errors isn't what's holding you back.

  • Did you forget to use break after each case statement in a switch statement?
  • Did you use the assignment operator = when you intended to use the comparison operator ==?
  • Are the termination conditions on your loops correct? Make sure that you're not terminating loops one iteration too early or too late. That is, make sure that 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 Chapter 2, Object-Oriented Programming Concepts (page 45).
  • Make sure that blocks of statements are enclosed in braces {}. The following code looks right because of indentation, but it doesn't do what the indents imply, because the braces 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 that 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 similar while 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? Note the extra semicolon at the end of this for statement:

    for (int i = 0; i < arrayOfInts.length; i++) ; 
     arrayOfInts[i] = i; 

Getting Started

Object-Oriented Programming Concepts

Language Basics

Object Basics and Simple Data Objects

Classes and Inheritance

Interfaces and Packages

Handling Errors Using Exceptions

Threads: Doing Two or More Tasks at Once

I/O: Reading and Writing

User Interfaces That Swing

Appendix A. Common Problems and Their Solutions

Appendix B. Internet-Ready Applets

Appendix C. Collections

Appendix D. Deprecated Thread Methods

Appendix E. Reference



The Java Tutorial(c) A Short Course on the Basics
The Java Tutorial: A Short Course on the Basics, 4th Edition
ISBN: 0321334205
EAN: 2147483647
Year: 2002
Pages: 125

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net