无法解决java.lang.NullPointerException

时间:2013-02-05 11:44:00

标签: java spring junit persistence

我有三个班级 - 班级'ABC''PQR'和'XYZ'

ABC类

@Test
public void getModulesid() throws Exception {
    Long GrpId= PQR.getExistingGroupId();
    System.out.println(GrpId);
}

PQR类

public static Long getExistingGroupId() throws Exception {
    Long GrpId;
    List<MsecBusGroups> busGroupsList = new ArrayList<MsecBusGroups>();
    busGroupsList=XYZ.getBusGroups();
    if (busGroupsList!=null){
        MsecBusGroups It1=busGroupsList.get(busGroupsList.size()-1);
        GrpId=It1.getId();
        return GrpId;
    } else {
        throw new Exception("BUSINESS GROUPS LIST RETURNED IS NULL");
    }
}

类XYZ

public List<MsecBusGroups> getBusGroups() throws PersistenceException {

    final String METHOD_NAME = "getBusGroups()";
    LogHelper.logEntry(CLASS_NAME, METHOD_NAME);
    List<MsecBusGroups> list = null;
    try {
        Query q = em.createNamedQuery("getBusGroups");
        list = q.getResultList();
        if (list == null) {
            LogHelper.debug(CLASS_NAME, METHOD_NAME, "No Record   Found");
        }
    ...
}

当我在ABC类中运行测试方法时,它会抛出java.lang.NullPointerException

java.lang.NullPointerException
    at com.causeway.platform.security.model.actions.BusServiceActions.getExistingGroupId(BusServiceActions.java:202)
    at com.causeway.platform.security.model.serviceImpl.SecurityBusServiceTest.testDeleteModule(SecurityBusServiceTest.java:1231)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

2 个答案:

答案 0 :(得分:1)

我看到getBusGroups()不是静态的。这意味着在busGroupsList=XYZ.getBusGroups(); XYZ中是一个变量。 NPE可能被抛出,因为XYZ为空。

答案 1 :(得分:-1)

securityBusService可能是null。它在哪里初始化?