Java "ExceptionInInitializerError" in CI - Fix the Static Initializer
ExceptionInInitializerError wraps any exception thrown during a class's static initialization (<clinit>) - a static field assignment or static block that failed. The wrapper is generic; the Caused by is what matters.
What this error means
A test or run fails with java.lang.ExceptionInInitializerError and a Caused by: such as an NPE, a missing config property, or a failed resource load during static setup. Subsequent loads of the same class throw NoClassDefFoundError.
java.lang.ExceptionInInitializerError
at com.example.AppTest.start(AppTest.java:10)
Caused by: java.lang.NullPointerException: env var DB_URL is not set
at com.example.Config.<clinit>(Config.java:7)Common causes
A static block or field threw
Static initialization that reads env vars, files, or system properties not present in CI throws, and the JVM wraps it. Local runs that have those values pass.
A dependency failed to initialize statically
A library whose static init requires configuration or a native resource can fail the same way during class loading.
How to fix it
Read the Caused by and fix the root
The wrapped exception names the exact line and cause; provide what the static init needs in CI.
# The Caused by said DB_URL was missing during static init.
# Provide it (or move the read out of <clinit>):
env:
DB_URL: jdbc:postgresql://localhost:5432/testAvoid heavy work in static initializers
- Move environment/config reads out of
<clinit>into lazy initialization so failures are recoverable and testable. - Provide required system properties / env vars in the CI job.
- Watch for a later NoClassDefFoundError on the same class - that is a symptom, not the cause.
How to prevent it
- Keep static initializers trivial, read configuration lazily, and supply required env/properties in CI so class loading never throws.