Exception Inwards Thread Brain Java.Lang.Exceptionininitializererror Inwards Coffee Program

JVM throws java.lang.ExceptionInInitializerError, when at that topographic point is an Exception within static initializer block. If you lot know most static variable inwards Java, hence you lot may know that they are initialized at the fourth dimension of shape loading. If at that topographic point is an Exception during that initialization of static variables, you lot volition encounter ExceptionInInitializerError in Java. This could last whatsoever exception e.g. java.lang.ArrayIndexOutOfBound or java.lang.NullPointerException. Java developers frequently confused amongst this fault because, they think that they receive got non defined whatsoever static initializer block, hence how come upwards they are getting ExceptionInInitializerError; well, yesteryear default Java combines all static variable initialization within a static initializer block together with initialize them inwards the lodge they are declared inwards root file.

If suppose a variable ABC is declared at line of piece of work 1, used at line of piece of work 2 only initialized at line of piece of work 3, hence code at line of piece of work 2 volition throw java.lang.NullPointerException, which volition last wrapped yesteryear JVM inwards ExceptionInInitializerError, together with if that code happens to last executed yesteryear brain thread hence you lot volition encounter "Exception inwards thread "main" java.lang.ExceptionInInitializerError" inwards your console or log file.

In a large application amongst huge log files one-time this fault got unnoticed, together with programmers acquire alerted yesteryear dreaded java.lang.NoClassDefFoundError. Unfortunately this fault comes when customer shape tries to purpose the class, which was non loaded because of ExceptionInInitializerError . Since shape loading was failed earlier, JVM is straightaway throwing NoClassDefFoundError.

Sometimes this misleads Java developer, together with they start looking at classpath, path together with java.library.path for missing class, together with confused them amongst hell non finding whatsoever anomalies.  If you lot are investigating travail of NoClassDefFoundError, it's e'er a ameliorate persuasion to cheque 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 together with 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 whatsoever other fault or exception, yesteryear showtime looking at this line, you lot know that exception is java.lang.ExceptionInInitializerError, which comes because of failure during class loading together with static initialization. Since it has occurred within Main thread, which is responsible for starting application, it’s ameliorate to start your investigation from Main class, the shape which you lot provided to java command at ascendance line of piece of work or same shape where you lot receive got written your public static void main(String args[]) method. Now if you lot await at amount stacktrace carefully, you lot don't request to practise anything because JVM prints refer of the class, which caused ExceptionInInitializerError. It's likewise a subclass of LinkageError, which way if this fault has occurred hence you lot shape volition non last loaded into JVM memory. Now let's accept a await at our lawsuit program, which upon execution, throwing next fault :

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 fault is java.lang.IndexOutOfBoundsException, which came at line of piece of work 12 of StaticInitiazerDemo class. It came because of telephone telephone to get() method of ArrayList with index 0 together with come upwards because size of ArrayList was likewise cypher (Index: 0, Size: 0 business office of stack-trace). Now yesteryear next this information, you lot know that our List<CreditCard> is empty, when nosotros travail to acquire showtime CreditCard from this list.

import java.util.ArrayList; import java.util.List;  /**  * Java Program to sympathise together with solve ExceptionInitializerError, which comes  * When static initializer blocks throws unchecked exception during shape loading  * together with 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 depository fiscal establishment      */     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 shape hierarchy of all Error shape inwards Java. You tin encounter that ExceptionInInitializerError inherit from LinkageError. Its likewise worth knowing that similar RuntimeException, Errors are likewise unchecked together with compiler doesn't cheque for mandatory fault treatment code.
 If you lot know most static variable inwards Java Exception inwards thread brain 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, together with it is java.lang.ExceptionInInitializerError, which is sub-class of LinkageError and comes when JVM tries to charge a shape together with it failed because of whatsoever RuntimeException in static initializer block e.g. IndexOutOfBoundsException or NullPointerException.

2) Remember that JVM combines all static variable initialization into 1 static initializer block inwards the lodge they appear inwards root file. So, don't think that absence of explicit static initializer block volition non travail this error. In fact, you lot must ensure right lodge of static variables i.e. if 1 variable initialization uses around other variable hence brand certain that is initialized first.

3) Down the line, java.lang.ExceptionInInitializerError tin travail ClassNotFoundException or NoClassDefFoundError, if around other code tries to purpose the class, which caused ExceptionInInitializerError. Why? because loading of that shape is failed together with it’s non available within JVM memory. So e'er cheque your log files for this before fifty-fifty if you lot are looking to solve whatsoever work related to shape non found.

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

That's all most "Exception inwards thread "main" java.lang.ExceptionInInitializerError". We receive got learned how to troubleshoot this fault together with detect the existent culprit, which throwing this error. Always, scream upwards potential side lawsuit of this fault is NoClassDefFoundError, which your Java application may throw far from this error, depending upon when other customer code refer to this shape inwards question. So, it’s e'er ameliorate to await for ExceptionInInitializerError, before playing amongst ClassPath to troubleshoot NoClassDefFoundError inwards Java.

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


Sumber https://javarevisited.blogspot.com/

0 Response to "Exception Inwards Thread Brain Java.Lang.Exceptionininitializererror Inwards Coffee Program"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel