带有 jdbcTemplate 的奇怪空指针
Strange null pointer with jdbcTemplate
使用 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.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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 启动版本 1.3。3.Release、spring data Gosling-SR1、spring data commons 1.11.2 和 spring batch通过 spring-boot-starter-batch.
在某些情况下,您的查询 return 为空,因为您的查询过滤器不会匹配数据库中的任何记录。这将 return 一个空引用,然后将其分配给原始值 id_half。
Java 将尝试拆箱 null 并给出 NullPointerExcpeption。
像这样处理查询中缺少匹配记录(可能取决于数据库)
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;
}
}
使用 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.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.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.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=11=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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 启动版本 1.3。3.Release、spring data Gosling-SR1、spring data commons 1.11.2 和 spring batch通过 spring-boot-starter-batch.
在某些情况下,您的查询 return 为空,因为您的查询过滤器不会匹配数据库中的任何记录。这将 return 一个空引用,然后将其分配给原始值 id_half。
Java 将尝试拆箱 null 并给出 NullPointerExcpeption。
像这样处理查询中缺少匹配记录(可能取决于数据库)
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;
}
}