user2158382
user2158382

Reputation: 4510

Java weird NoClassDefFoundError

I keep getting a NoClassDefFoundError whenever I try to run this block of code

ClassTable.getInstance();

Here is the code for ClassTable

public class ClassTable
{
    private static ClassTable classTable = new ClassTable();
    private Map<String,Object> pricingTable;
    private Date expiry;


    private ClassTable()
    {
        this.expiry = this.getExpiry();
        this.pricingTable = buildPricingTables();
    }

    private Date getExpiry()
    {
        return DateUtils.INSTANCE.getCutOff();
    }

    public static ClassTable getInstance()
    {
      return classTable;
    }
}

I tried stepping into the method getInstance(); however, it throws the error instantly. Here is the stack trace

java.lang.NoClassDefFoundError: Could not initialize class helper.ClassTable
    at models.SinglePaymentLoan.getPricingTable(SinglePaymentLoan.java:742)
    at unit.SinglePaymentLoanTest.testPricingTable(SinglePaymentLoanTest.java:135)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at play.test.PlayJUnitRunner$StartPlay$2$1.evaluate(PlayJUnitRunner.java:114)
    at org.junit.rules.TestWatcher$1.evaluate(TestWatcher.java:47)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at play.test.PlayJUnitRunner.run(PlayJUnitRunner.java:58)
    at org.junit.runners.Suite.runChild(Suite.java:128)
    at org.junit.runners.Suite.runChild(Suite.java:24)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:157)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:136)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:117)
    at play.test.TestEngine.run(TestEngine.java:112)
    at controllers.TestRunner$1.doJobWithResult(TestRunner.java:71)
    at controllers.TestRunner$1.doJobWithResult(TestRunner.java:1)
    at play.jobs.Job.call(Job.java:146)
    at play.jobs.Job$1.call(Job.java:66)
    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:98)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:206)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:895)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:918)
    at java.lang.Thread.run(Thread.java:695)

Upvotes: 0

Views: 195

Answers (1)

Brett Okken
Brett Okken

Reputation: 6306

Note that it states that it cannot initialize the class. That indicates a problem with static initialization of the class.

You have one static variable classTable that attempts to create a new ClassTable instance.

private static ClassTable classTable = new ClassTable();
private Map<String,Object> pricingTable;
private Date expiry;

private ClassTable()
{
    this.expiry = this.getExpiry();
    this.pricingTable = buildPricingTables();
}

So now any error in creating a ClassTable instance will lead to a class initialization failure. You do 2 things in the constructor, each of which could fail.

You call getExpiry, which we see turns and calls DateUtils.INSTANCE.getCutOff(). You have not provided this code, so it is unclear what this does.

Finally, you call buildPricingTables(). Again, code has not been provided for this method.

So your 2 mostly likely culprits are DateUtils.INSTANCE.getCutOff() and buildPricingTables().

Upvotes: 2

Related Questions