JavaRush /Java Blog /Random EN /6 Java Exceptions That Haunt Newbies
gram2005
Level 30

6 Java Exceptions That Haunt Newbies

Published in the Random EN group
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. 6 Java Exceptions That Haunt Newbies - 1
  1. NoClassDefFoundError: This is one of those exceptions that Exception in thread “main“ NoClassDefFoundErroroften 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.

    NoClassDefFoundErrorhappens 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 value classpath. [ Classpathis 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 value classpath. If you wish, further study of this exception can be continued in the Javarevisited blog .
  2. ClassNotFoundException: Exception ClassNotFoundExceptionis 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 confusing ClassNotFoundExceptioneach NoClassDefFoundErrorother. 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.
    ClassNotFoundExceptionhappens when JVM tries to load a certain class and doesn't find the same in classpath. Usually beginners come across this in code that connects to a database using a JDBC library. Trying to load the driver with the following code Class.forName( “JDBCdriver”). Good material ClassNotFoundExceptioncan 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:

    1. When trying to load a class using a method Class.forNameand the file .classdoes not exist in the classpath. This is the most common of the three.
    2. When a class loader tries to load a class using the loadClass.
    3. When a class loader tries to load a class using findSystemClass.
  3. NullPointerException: the exception NullPointerExceptionis 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 to null. Other cases, as mentioned in the Java documentation, might be:

    1. By accessing or modifying a method on an object that is null.
    2. Getting the length of an array when it is null.
    3. When accessing or changing objects that are enclosed in an array that is null.
    The easiest way to avoid this exception is to add an inequality check 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 for nullis not considered an example of good programming. The main reason why checking against nullis used by developers is to pass an object nullin 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 for null. It's easier said than done, though. :)

  4. 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”.

  5. 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.

  6. 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.

Comments
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION