Adding null check for stat closing

Bug: 300996262
Test: atest FrameworksMockingServicesTests -- --abi x86_64
Change-Id: If85c705a85a6daf954e202aca409d6af1362ba85
diff --git a/services/core/java/com/android/server/am/AppBatteryTracker.java b/services/core/java/com/android/server/am/AppBatteryTracker.java
index 907069d..147f8d1 100644
--- a/services/core/java/com/android/server/am/AppBatteryTracker.java
+++ b/services/core/java/com/android/server/am/AppBatteryTracker.java
@@ -580,7 +580,11 @@
                             batteryStatsInternal);
             curDuration += curStart - lastUidBatteryUsageStartTs;
             try {
-                statsCommit.close();
+                if (statsCommit != null) {
+                    statsCommit.close();
+                } else {
+                    Slog.w(TAG, "Stat was null");
+                }
             } catch (IOException e) {
                 Slog.w(TAG, "Failed to close a stat");
             }
@@ -660,7 +664,11 @@
             }
         }
         try {
-            stats.close();
+            if (stats != null) {
+                stats.close();
+            } else {
+                Slog.w(TAG, "Stat was null");
+            }
         } catch (IOException e) {
             Slog.w(TAG, "Failed to close a stat");
         }
@@ -684,7 +692,11 @@
         final BatteryUsageStats stats = statsList.get(0);
         for (int i = 1; i < statsList.size(); i++) {
             try {
-                statsList.get(i).close();
+                if (statsList.get(i) != null) {
+                    statsList.get(i).close();
+                } else {
+                    Slog.w(TAG, "Stat was null");
+                }
             } catch (IOException e) {
                 Slog.w(TAG, "Failed to close a stat in BatteryUsageStats List");
             }