java 非空对象出现NullPointerException

pokxtpni  于 2023-02-07  发布在  Java
关注(0)|答案(1)|浏览(117)

我有个奇怪的问题:我正在调试我的应用程序,遇到了这个NullPointerException,但是对象不是空的。

java.lang.NullPointerException
    at com.clh.model.margin.table.classes.ChMarginHeaderP.createCopy(Unknown Source)
    at com.clh.controller.builders.EccFirstCallBean.buildDefaultComponent(EccFirstCallBean.java:710)
    at com.clh.controller.builders.EccFirstCallBean.buildFirstCall(EccFirstCallBean.java:549)
    at com.clh.controller.builders.EccFirstCallBean.buildMargin(EccFirstCallBean.java:403)
    at com.clh.controller.builders.EccFirstCallBean.elabThirdLevel(EccFirstCallBean.java:268)
    at com.clh.controller.builders.EccFirstCallBeanTest.elabThirdLevelTest(EccFirstCallBeanTest.java:529)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.internal.runners.TestMethod.invoke(TestMethod.java:68)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:326)
    at org.junit.internal.runners.MethodRoadie$2.run(MethodRoadie.java:89)
    at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:97)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:310)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:131)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.access$100(PowerMockJUnit47RunnerDelegateImpl.java:59)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner$TestExecutorStatement.evaluate(PowerMockJUnit47RunnerDelegateImpl.java:147)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.evaluateStatement(PowerMockJUnit47RunnerDelegateImpl.java:107)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:298)
    at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:87)
    at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:50)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:218)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:160)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:134)
    at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:34)
    at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:44)
    at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:136)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:117)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:57)
    at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:542)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:770)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:464)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:210)

这就是我按F5键进入并获取异常之前的情况:

这是创建复制方法:

public ChMarginHeaderP createCopy() {
        final ChMarginHeaderP copyChMarginHeaderP = new ChMarginHeaderP(
                new ChMarginHeaderPPK(1), this.flgLock,this.flgIsLive, 
                this.referenceDate, this.userDescription,
                this.chCounterpartyPk,
                this.chCurrencyPk, this.chTypePk,
                this.chPortfolioPk, this.chProductsPk, this.chEntitiesPk,
                this.chStatusTradePk, this.chTypeCalledPk,this.segment, this.class_, this.section, this.division);

        return copyChMarginHeaderP;
    }

super()方法属于Object类

public ChMarginHeaderP(final ChMarginHeaderPPK id, final boolean flgLock,
            final boolean flgLive, final Date referenceDate,
            final String userDescription, final String chCounterpartiesPk,
            final String chCurrenciesPk, final String chTypePk,
            final String chPortfolioPk, final String chProductsPk,
            final String chEntitiesPk, final String chStatusMargin,
            final String chTypeCalledPk, final String segment, 
            final String class_, final String section, final String division) {
        super();
        this.id = id;
        this.flgLock = flgLock;
        this.flgIsLive = flgLive;
        this.referenceDate = referenceDate;
        this.userDescription = userDescription;
        this.chCounterpartyPk= chCounterpartiesPk;
        this.chCurrencyPk = chCurrenciesPk;
        this.chTypePk = chTypePk;
        this.chPortfolioPk = chPortfolioPk;
        this.chProductsPk = chProductsPk;
        this.chEntitiesPk = chEntitiesPk;
        this.chStatusTradePk = chStatusMargin;
        this.chTypeCalledPk = chTypeCalledPk;
        this.segment = segment;
        this.class_ = class_;
        this.section = section; 
        this.division = division;
    }

为什么我的调试器看不到方法的源代码?可能的原因是什么?“添加行号属性...”在首选项的编译器选项中被选中。我也尝试过删除所有断点,清理项目,取消选中,然后选中该框,但我做不到。

rdlzhqv9

rdlzhqv91#

堆栈跟踪的第一行是实际抛出NPE的地方,正如您所看到的,它而不是在您突出显示的那一行--这是您调用createCopy()的地方,它工作正常(因为defaultChMarginHeaderP不为空,正如调试器正确告诉您的那样)。createCopy()调用 * 内部 * 的一些代码要么显式抛出异常(实际上,throw new NullPointerException()在该方法中并且正在执行),或者,其中的一些代码取消引用空指针。通常类似于foo.methodCall(),其中foonull。这是什么代码?不知道-您正在将ChMarginHeaderP作为类文件运行(不是作为源文件),并且类文件没有调试信息,因此,行号未知等等。
在没有调试信息的情况下进行调试意味着您必须阅读文档并四处走动,"在黑暗中“弄清楚您违反了createCopy方法的哪些前提条件要求。
通常你有这个信息,它给你一个行号,你可以交叉检查ChMarginHeaderP的源代码,然后你会知道得更快。

相关问题