-
NoClassDefFoundError
: This is one of those exceptions thatException in thread “main“ NoClassDefFoundError
often welcomes new developers into the world of Java programming with a message. They write a helloworld program, go to the command line and write " java " - the command to execute and "BAM" :-) In a little while, beginners will understand how to fix this exception and see how their hello world is output.NoClassDefFoundError
happens when the Java Virtual Machine (JVM) tries to access a class at run time and the class is not found, although the same class was at compile time. Most often, this exception occurs when running a Java program through the "java" command with an invalid parameter valueclasspath
. [Classpath
is a parameter that is set via the command line or via an environment variable that tells the Java virtual machine or Java compiler where to look for classes or packages declared by the user - approx. translator] Possible reasons for exclusion:- The class is not available in
Classpath
. - Often a script that is executed when the operating system starts up changes the value of the environment variable
classpath
. This can be checked by executing the “set
” command on the Windows command line and seeing if the class definition is included in the valueclasspath
. If you wish, further study of this exception can be continued in the Javarevisited blog .
- The class is not available in
-
ClassNotFoundException
: ExceptionClassNotFoundException
is another exception that gives newbies just starting out Java programming nightmares. It is interesting that for the average developer it takes some time to stop confusingClassNotFoundException
eachNoClassDefFoundError
other. And so the question of the difference between these two exceptions remains one of the frequently asked in the interview for the position of junior Java developer.ClassNotFoundException
happens when JVM tries to load a certain class and doesn't find the same inclasspath
. Usually beginners come across this in code that connects to a database using a JDBC library. Trying to load the driver with the following codeClass.forName( “JDBCdriver”)
. Good materialClassNotFoundException
can be found here. It is also recommended that you familiarize yourself with and understand the concept of class loaders in Java in order to deal effectively with this exception. You might want to see the next page on how to set up the classpath in Win/Unix environments . And so, as follows from the Java documentation, this exception happens in the following cases:- When trying to load a class using a method
Class.forName
and the file.class
does not exist in theclasspath
. This is the most common of the three. - When a class loader tries to load a class using the
loadClass
. - When a class loader tries to load a class using
findSystemClass
.
- When trying to load a class using a method
-
NullPointerException
: the exceptionNullPointerException
is easier to understand and beginners deal with it faster than the previous two. At the same time, the reason for the exception is very easy to find, since the line number where it happened is given. First of all, an exception occurs when the JVM tries to access null in the place where the object should have been. Most often this happens when the JVM tries to call a method using an object and it turns out that the object is equal tonull
. Other cases, as mentioned in the Java documentation, might be:- By accessing or modifying a method on an object that is
null
. - Getting the length of an array when it is
null
. - When accessing or changing objects that are enclosed in an array that is
null
.
null
. Therefore, sooner or later the developer starts using it, and then checks for null appear everywhere. It is interesting that to use everywhere check fornull
is not considered an example of good programming. The main reason why checking againstnull
is used by developers is to pass an objectnull
in case of an error. Instead, an example of good programming that should be promoted by programmers is to pass an empty object when a condition for the main/working branch of a program is not satisfied. This in turn would reduce the number of checks fornull
. It's easier said than done, though. :) - By accessing or modifying a method on an object that is
ClassCastException
: This is another one of the exceptions newbies encounter when they try to cast an object to a class of which it is not an instance. Again, it is quite easy to understand, find the cause and fix it. One way to avoid this exception, when the type of an object is not known at runtime, is to use a check to see if the object is an instance of a certain class: “instanceof
”.ArrayIndexOutOfBoundsException
: The name of this exception speaks for itself. It happens when the JVM tries to access an element of an array using an invalid index, such as negative (-1) or greater than or equal to the size of the array. It is easy to understand, find the cause and fix. The following code example will help avoid this exception:for( index = 0; index < array.length; index++ )
Note that the index starts at 0 and increases to a value 1 less than the size of the array.-
IllegalArgumentException
: This exception is the simplest, it is easy to understand, find its cause and fix it. It happens when the JVM tries to pass an invalid argument or an argument of the wrong type to a method.
gram2005
Level 30
6 Java Exceptions That Haunt Newbies
From time to time I run into novice developers who get stuck on understanding the following simple exceptions and I have to re-explain everything to them. Most likely, many experienced Java developers find themselves in a similar situation and help newcomers deal with the following exceptions. Therefore, I decided to write this article and refer to it in the future. Comments and additions to the exclusion list are welcome.
GO TO FULL VERSION