From 1a41252ff91ce497c4b117dff87defc5abda5e28 Mon Sep 17 00:00:00 2001 From: comecny <48578879+comecny@users.noreply.github.com> Date: Wed, 4 Sep 2024 12:17:40 +0800 Subject: [PATCH] NPE occurs when execute show table status (#32776) * Add an early return when the object is null * Return result is null * Code Style * optimize code * optimize code * optimize code * fix --- .../dal/show/ShowTableStatusMergedResult.java | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResult.java b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResult.java index cfee237eff968..4f3ae659cd2aa 100644 --- a/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResult.java +++ b/features/sharding/core/src/main/java/org/apache/shardingsphere/sharding/merge/dal/show/ShowTableStatusMergedResult.java @@ -74,10 +74,24 @@ private void merge(final MemoryQueryResultRow row, final MemoryQueryResultRow ne } private BigInteger sum(final Object num1, final Object num2) { - return ((BigInteger) num1).add((BigInteger) num2); + if (num1 == null && num2 == null) { + return null; + } + return getNonNullBigInteger(num1).add(getNonNullBigInteger(num2)); } private BigInteger avg(final Object sum, final Object number) { - return BigInteger.ZERO.equals(number) ? BigInteger.ZERO : ((BigInteger) sum).divide((BigInteger) number); + if (sum == null && number == null) { + return null; + } + BigInteger numberBigInteger = getNonNullBigInteger(number); + return BigInteger.ZERO.equals(numberBigInteger) ? BigInteger.ZERO : getNonNullBigInteger(sum).divide(numberBigInteger); + } + + private BigInteger getNonNullBigInteger(final Object value) { + return Optional.ofNullable(value) + .filter(BigInteger.class::isInstance) + .map(BigInteger.class::cast) + .orElse(BigInteger.ZERO); } }