Exception Inward Thread Primary Java.Lang.Exceptionininitializererror Inward Coffee Program

JVM throws java.lang.ExceptionInInitializerError, when at that spot is an Exception within static initializer block. If yous know virtually static variable inwards Java, therefore yous may know that they are initialized at the fourth dimension of class loading. If at that spot is an Exception during that initialization of static variables, yous volition encounter ExceptionInInitializerError in Java. This could live on whatever exception e.g. java.lang.ArrayIndexOutOfBound or java.lang.NullPointerException. Java developers ofttimes confused amongst this mistake because, they think that they receive got non defined whatever static initializer block, therefore how come upward they are getting ExceptionInInitializerError; well, past times default Java combines all static variable initialization within a static initializer block in addition to initialize them inwards the gild they are declared inwards beginning file.

If suppose a variable ABC is declared at line of piece of work 1, used at line of piece of work 2 simply initialized at line of piece of work 3, therefore code at line of piece of work 2 volition throw java.lang.NullPointerException, which volition live on wrapped past times JVM inwards ExceptionInInitializerError, in addition to if that code happens to live on executed past times master copy thread therefore yous volition encounter "Exception inwards thread "main" java.lang.ExceptionInInitializerError" inwards your console or log file.

In a large application amongst huge log files quondam this mistake got unnoticed, in addition to programmers acquire alerted past times dreaded java.lang.NoClassDefFoundError. Unfortunately this mistake comes when customer class tries to operate the class, which was non loaded because of ExceptionInInitializerError . Since class loading was failed earlier, JVM is immediately throwing NoClassDefFoundError.

Sometimes this misleads Java developer, in addition to they start looking at classpath, path in addition to java.library.path for missing class, in addition to confused them amongst hell non finding whatever anomalies.  If yous are investigating crusade of NoClassDefFoundError, it's ever a meliorate consider to banking concern agree your application log files for ExceptionInInitializerError before looking at CLASSPATH.

In this article, nosotros volition encounter an lawsuit code, which generates exception during static initialization in addition to results inwards "Exception inwards thread "main" java.lang.ExceptionInInitializerError". In afterwards part, nosotros volition encounter how to create this error.




Cause of "Exception inwards thread "main" java.lang.ExceptionInInitializerError"

As amongst whatever other mistake or exception, past times commencement looking at this line, yous know that exception is java.lang.ExceptionInInitializerError, which comes because of failure during class loading in addition to static initialization. Since it has occurred within Main thread, which is responsible for starting application, it’s meliorate to start your investigation from Main class, the class which yous provided to java command at ascendency line of piece of work or same class where yous receive got written your public static void main(String args[]) method. Now if yous await at sum stacktrace carefully, yous don't take away to practice anything because JVM prints elevate of the class, which caused ExceptionInInitializerError. It's likewise a subclass of LinkageError, which way if this mistake has occurred therefore yous class volition non live on loaded into JVM memory. Now let's accept a await at our lawsuit program, which upon execution, throwing next mistake :

Exception in thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0         at java.util.ArrayList.rangeCheck(ArrayList.java:635)         at java.util.ArrayList.get(ArrayList.java:411)         at StaticInitiazerDemo.<clinit>(StaticInitiazerDemo.java:15)

By looking at this stack trace, nosotros know that actual mistake is java.lang.IndexOutOfBoundsException, which came at line of piece of work 12 of StaticInitiazerDemo class. It came because of telephone band to get() method of ArrayList with index 0 in addition to come upward because size of ArrayList was likewise nothing (Index: 0, Size: 0 business office of stack-trace). Now past times next this information, yous know that our List<CreditCard> is empty, when nosotros endeavour to acquire commencement CreditCard from this list.

import java.util.ArrayList; import java.util.List;  /**  * Java Program to sympathize in addition to solve ExceptionInitializerError, which comes  * When static initializer blocks throws unchecked exception during class loading  * in addition to initialization.  *  * @author Javin Paul  */  public class StaticInitializerDemo{      private static final List<CreditCard> cards = new ArrayList<CreditCard>();     private static CreditCard prefferdCard = cards.get(0); // 1st carte du jour is default     public static boolean isVisa = "VISA".equalsIgnoreCase(prefferdCard.getNetwork());      public static void main(String args[]) {          makePayment(prefferdCard);      }      public static void makePayment(CreditCard cc) {         if (isVisa) {             //offer 5% discount         }         // deduct payment     }  }  class CreditCard {      private long card_number; //16 digit carte du jour number     private int cvv; // iii digit cvv number     private int expiryMonth;     private int expiryYear;     private String bank;     private String network;      public CreditCard(long card_number, int cvv, int expiryMonth, int expiryYear, String bank, String network) {         super();         this.card_number = card_number;         this.cvv = cvv;         this.expiryMonth = expiryMonth;         this.expiryYear = expiryYear;         this.bank = bank;         this.network = network;      }      /**      * @return the card_number      */     public final long getCard_number() {         return card_number;     }     /**      * @return the cvv      */     public final int getCvv() {         return cvv;     }     /**      * @return the expiryMonth      */     public final int getExpiryMonth() {         return expiryMonth;     }     /**      * @return the expiryYear      */     public final int getExpiryYear() {         return expiryYear;     }     /**      * @return the banking concern      */     public final String getBank() {         return bank;     }     /**      * @return the network      */     public final String getNetwork() {         return network;     } }  Output Exception inwards thread "main" java.lang.ExceptionInInitializerError Caused by: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0     at java.util.ArrayList.rangeCheck(Unknown Source)     at java.util.ArrayList.get(Unknown Source)     at StaticInitializerDemo.<clinit>(StaticInitializerDemo.java:15)

Here is class hierarchy of all Error class inwards Java. You tin give the axe encounter that ExceptionInInitializerError inherit from LinkageError. Its likewise worth knowing that similar RuntimeException, Errors are likewise unchecked in addition to compiler doesn't banking concern agree for mandatory mistake treatment code.
 If yous know virtually static variable inwards Java Exception inwards thread master copy java.lang.ExceptionInInitializerError inwards Java Program

Things to remember:

1) Remember "Exception inwards thread "main" java.lang.ExceptionInInitializerError" way Exception has occurred inwards main thread, in addition to it is java.lang.ExceptionInInitializerError, which is sub-class of LinkageError and comes when JVM tries to charge a class in addition to it failed because of whatever RuntimeException in static initializer block e.g. IndexOutOfBoundsException or NullPointerException.

2) Remember that JVM combines all static variable initialization into ane static initializer block inwards the gild they appear inwards beginning file. So, don't think that absence of explicit static initializer block volition non crusade this error. In fact, yous must ensure right gild of static variables i.e. if ane variable initialization uses approximately other variable therefore brand certain that is initialized first.

3) Down the line, java.lang.ExceptionInInitializerError tin give the axe crusade ClassNotFoundException or NoClassDefFoundError, if approximately other code tries to operate the class, which caused ExceptionInInitializerError. Why? because loading of that class is failed in addition to it’s non available within JVM memory. So ever banking concern agree your log files for this before fifty-fifty if yous are looking to solve whatever employment related to class non found.

4) Remember Static initializer block tin give the axe throw RuntimeException simply non checked Exception, because afterwards required mandatory grab block for handling.

That's all virtually "Exception inwards thread "main" java.lang.ExceptionInInitializerError". We receive got learned how to troubleshoot this mistake in addition to abide by the existent culprit, which throwing this error. Always, recollect potential side lawsuit of this mistake is NoClassDefFoundError, which your Java application may throw far from this error, depending upon when other customer code refer to this class inwards question. So, it’s ever meliorate to await for ExceptionInInitializerError, before playing amongst ClassPath to troubleshoot NoClassDefFoundError inwards Java.

Further Learning
Understanding the Java Virtual Machine: Class Loading in addition to Reflection
Java Performance The Definitive Guide By Scott Oaks
Understanding in addition to Solving Java Memory Problems

Belum ada Komentar untuk "Exception Inward Thread Primary Java.Lang.Exceptionininitializererror Inward Coffee Program"

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel