Common Errors

For years I have joked with students about the "top ten" errors that novice Java programmers make. These are often simple things that make it through the compiler but cause the code to not work the way you expected. This page will be a place for you to record these kinds of errors and will serve as a good starting point for you to look for ideas when your code isn't behaving properly.

Error Description Code Example What's wrong? How do I fix it?
File/class name mismatch public class Problem {} Class name and file name must be the same
java Class results in long error but compiled Says something about missing class run with -cp . args: java -cp . Class
defining a local variable with the same name as instance variable private int friction; new FrictionBall(int friction){ friction = friction; } once the method is done friction is still null; FIX if refering to the instance variable you need to use this.(instance variable)
j-unit test files don't build bunch of errors on student.cs -cp .:junit-4.5.jar (NOT ;)
NullPointerException char[] name = new char[3]; If name isn't given an initial value a null value could be used later in code. Each char in the array should be initialized to '' or ' ' to stop this.
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License