带有jdbcTemplate的奇怪空指针

时间:2016-05-06 11:56:20

标签: java spring spring-data spring-batch

使用jdbcTemplate时,一个奇怪的NullPointer会让我烦恼,我无法找到NullPointerException的来源。

来源代码段:

@Slf4j
public class ApproximationBuilder {

  // [...]

  public static DistributionApproximation buildQuadraticApproximation(JdbcOperations jdbcTemplate, String table, String column) {
    int id_min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL", Integer.class);
    int id_max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL", Integer.class);
    int id_half = 0;

    try {
        id_half = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL " + " and " + column + " < " + Math.ceil((id_max - id_min) / 2.0), Integer.class); 
        // The above line is number 36
    } catch (NullPointerException e) {
        // THIS SHOULD NOT HAPPEN!
        log.error("Null Pointer! " + e.getMessage());
        id_half = (id_max - id_min) / 2;
    }

    // ...
}

如您所见,以前使用过所有参数,唯一不同的是最后的附加条件。此外,这适用于很多场景。只有一个(与其余的类似)它会导致空指针。

不幸的是,堆栈跟踪并没有真正帮助解决问题(删除了try / catch块):

    2016-05-06 13:51:14,352 [adnb0007] [main] ERROR o.s.batch.core.step.AbstractStep - Encountered an error executing step prepareDeviceDimensionWrapper in job Load_Devices_Cube
java.lang.NullPointerException: null
    at com.aspera.dev.sim.loader.util.partitioning.approximations.ApproximationBuilder.buildQuadraticApproximation(ApproximationBuilder.java:36)
    at com.aspera.dev.sim.loader.util.ColumnRangePartitioner.partition(ColumnRangePartitioner.java:85)
    at org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter.getContexts(SimpleStepExecutionSplitter.java:214)
    at org.springframework.batch.core.partition.support.SimpleStepExecutionSplitter.split(SimpleStepExecutionSplitter.java:177)
    at org.springframework.batch.core.partition.support.AbstractPartitionHandler.handle(AbstractPartitionHandler.java:59)
    at org.springframework.batch.core.partition.support.PartitionStep.doExecute(PartitionStep.java:106)
    at org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:200)
    at org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)
    at org.springframework.batch.core.job.flow.JobFlowExecutor.executeStep(JobFlowExecutor.java:64)
    at org.springframework.batch.core.job.flow.support.state.StepState.handle(StepState.java:67)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.resume(SimpleFlow.java:169)
    at org.springframework.batch.core.job.flow.support.SimpleFlow.start(SimpleFlow.java:144)
    at org.springframework.batch.core.job.flow.FlowJob.doExecute(FlowJob.java:134)
    at org.springframework.batch.core.job.AbstractJob.execute(AbstractJob.java:306)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher$1.run(SimpleJobLauncher.java:135)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:128)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:302)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:190)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
    at org.springframework.batch.core.configuration.annotation.SimpleBatchConfiguration$PassthruAdvice.invoke(SimpleBatchConfiguration.java:127)
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
    at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:208)
    at com.sun.proxy.$Proxy74.run(Unknown Source)
    at org.springframework.batch.test.JobLauncherTestUtils.launchJob(JobLauncherTestUtils.java:152)
    at com.aspera.dev.sim.loader.AbstractIntegrationTest.launchJobTest(AbstractIntegrationTest.java:111)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:75)
    at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:86)
    at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:254)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

我正在使用spring boot版本1.3.3。请发布弹簧数据Gosling-SR1,弹簧数据公共1.11.2和弹簧批量通过spring-boot-starter-batch。

1 个答案:

答案 0 :(得分:1)

在某些情况下,查询返回null,因为查询过滤器不匹配数据库中的任何记录。这将返回一个空引用,然后将其赋值给原始值id_half。

Java将尝试取消装入NullPointerExcpeption的null。

在查询中处理匹配记录的缺失(可能与DB有关),如此

id_half = jdbcTemplate.queryForObject("SELECT COALESCE(MAX(" + column + ") , 0) from " + table + " WHERE " + column + " IS NOT NULL " + " and " + column + " < " + Math.ceil((id_max - id_min) / 2.0), Integer.class); 

或者在Java代码中处理null

public static DistributionApproximation buildQuadraticApproximation(JdbcOperations jdbcTemplate, String table, String column) {
    int id_min = jdbcTemplate.queryForObject("SELECT MIN(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL", Integer.class);
    int id_max = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL", Integer.class);
    Integer id_half = 0;

    try {
        id_half = jdbcTemplate.queryForObject("SELECT MAX(" + column + ") from " + table + " WHERE " + column + " IS NOT NULL " + " and " + column + " < " + Math.ceil((id_max - id_min) / 2.0), Integer.class); 
        if(id_half == null){
           id_half = (id_max - id_min) / 2;
        }
    }