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

Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program - Hallo sahabat BEST LEARNING JAVA, Pada Artikel yang anda baca kali ini dengan judul Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program, kami telah mempersiapkan artikel ini dengan baik untuk anda baca dan ambil informasi didalamnya. mudah-mudahan isi postingan Artikel core java, Artikel error and exception, Artikel programming, yang kami tulis ini dapat anda pahami. baiklah, selamat membaca.

Judul : Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program
link : Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program

Baca juga


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

JVM throws java.lang.ExceptionInInitializerError, when at that topographic point is an Exception within static initializer block. If y'all know close static variable inward Java, thus y'all may know that they are initialized at the fourth dimension of course of education loading. If at that topographic point is an Exception during that initialization of static variables, y'all volition run into ExceptionInInitializerError in Java. This could survive whatever exception e.g. java.lang.ArrayIndexOutOfBound or java.lang.NullPointerException. Java developers oft confused amongst this mistake because, they think that they receive got non defined whatever static initializer block, thus how come upward they are getting ExceptionInInitializerError; well, yesteryear default Java combines all static variable initialization within a static initializer block as well as initialize them inward the social club they are declared inward root 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, thus code at line of piece of work 2 volition throw java.lang.NullPointerException, which volition survive wrapped yesteryear JVM inward ExceptionInInitializerError, as well as if that code happens to survive executed yesteryear principal thread thus y'all volition run into "Exception inward thread "main" java.lang.ExceptionInInitializerError" inward your console or log file.

In a large application amongst huge log files one-time this mistake got unnoticed, as well as programmers acquire alerted yesteryear dreaded java.lang.NoClassDefFoundError. Unfortunately this mistake comes when customer course of education tries to usage the class, which was non loaded because of ExceptionInInitializerError . Since course of education loading was failed earlier, JVM is instantly throwing NoClassDefFoundError.

Sometimes this misleads Java developer, as well as they start looking at classpath, path as well as java.library.path for missing class, as well as confused them amongst hell non finding whatever anomalies.  If y'all are investigating motility of NoClassDefFoundError, it's ever a ameliorate sentiment to cheque your application log files for ExceptionInInitializerError before looking at CLASSPATH.

In this article, nosotros volition run into an lawsuit code, which generates exception during static initialization as well as results inward "Exception inward thread "main" java.lang.ExceptionInInitializerError". In afterward part, nosotros volition run into how to create this error.




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

As amongst whatever other mistake or exception, yesteryear get-go looking at this line, y'all know that exception is java.lang.ExceptionInInitializerError, which comes because of failure during class loading as well as 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 course of education which y'all provided to java command at dominance line of piece of work or same course of education where y'all receive got written your public static void main(String args[]) method. Now if y'all expect at amount stacktrace carefully, y'all don't require to practise 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 thus y'all course of education volition non survive loaded into JVM memory. Now let's accept a expect 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 weep upward to get() method of ArrayList with index 0 as well as come upward because size of ArrayList was likewise zippo (Index: 0, Size: 0 purpose of stack-trace). Now yesteryear next this information, y'all know that our List<CreditCard> is empty, when nosotros endeavor to acquire get-go CreditCard from this list.

import java.util.ArrayList; import java.util.List;  /**  * Java Program to sympathize as well as solve ExceptionInitializerError, which comes  * When static initializer blocks throws unchecked exception during course of education loading  * as well as initialization.  *  * @author Javin Paul  */  public class StaticInitializerDemo{      private static final List<CreditCard> cards = new ArrayList<CreditCard>();     private static CreditCard prefferdCard = cards.get(0); // 1st menu 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 menu 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 inward 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 course of education hierarchy of all Error course of education inward Java. You tin run into that ExceptionInInitializerError inherit from LinkageError. Its likewise worth knowing that similar RuntimeException, Errors are likewise unchecked as well as compiler doesn't cheque for mandatory mistake treatment code.
 If y'all know close static variable inward Java Exception inward thread principal java.lang.ExceptionInInitializerError inward Java Program

Things to remember:

1) Remember "Exception inward thread "main" java.lang.ExceptionInInitializerError" way Exception has occurred inward main thread, as well as it is java.lang.ExceptionInInitializerError, which is sub-class of LinkageError and comes when JVM tries to charge a course of education as well as 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 inward the social club they appear inward root file. So, don't think that absence of explicit static initializer block volition non motility this error. In fact, y'all must ensure right social club of static variables i.e. if ane variable initialization uses to a greater extent than or less other variable thus brand certain that is initialized first.

3) Down the line, java.lang.ExceptionInInitializerError tin motility ClassNotFoundException or NoClassDefFoundError, if to a greater extent than or less other code tries to usage the class, which caused ExceptionInInitializerError. Why? because loading of that course of education is failed as well as it’s non available within JVM memory. So ever cheque your log files for this before fifty-fifty if y'all are looking to solve whatever occupation related to course of education non found.

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

That's all close "Exception inward thread "main" java.lang.ExceptionInInitializerError". We receive got learned how to troubleshoot this mistake as well as uncovering the existent culprit, which throwing this error. Always, think 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 course of education inward question. So, it’s ever ameliorate to expect for ExceptionInInitializerError, before playing amongst ClassPath to troubleshoot NoClassDefFoundError inward Java.

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



Demikianlah Artikel Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program

Sekianlah artikel Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program kali ini, mudah-mudahan bisa memberi manfaat untuk anda semua. baiklah, sampai jumpa di postingan artikel lainnya.

Anda sekarang membaca artikel Exception Inwards Thread Primary Java.Lang.Exceptionininitializererror Inwards Coffee Program dengan alamat link https://bestlearningjava.blogspot.com/2019/01/exception-inwards-thread-primary.html

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

Posting Komentar

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel