每当我尝试运行此代码块时,我都会一直得到NoClassDefFoundError

ClassTable.getInstance();


这是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;
    }
}


我尝试进入方法getInstance();但是,它会立即引发错误。这是堆栈跟踪

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)

最佳答案

请注意,它指出无法初始化该类。这表明该类的静态初始化存在问题。

您有一个静态变量classTable试图创建一个新的ClassTable实例。

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

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


因此,现在在创建ClassTable实例时出现任何错误都将导致类初始化失败。您在构造函数中做了两件事,每件事都会失败。

您呼叫getExpiry,我们看到转弯并呼叫DateUtils.INSTANCE.getCutOff()。您尚未提供此代码,因此尚不清楚其作用。

最后,您调用buildPricingTables()。同样,没有为该方法提供代码。

因此,您的2个最可能的罪魁祸首是DateUtils.INSTANCE.getCutOff()buildPricingTables()

09-28 11:42