This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-lang.git


The following commit(s) were added to refs/heads/master by this push:
     new 677e57f56 Fix NullPointerException in StopWatch.getStopTime()
677e57f56 is described below

commit 677e57f56bfae93af520637d47a615013c930266
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Mar 4 14:29:18 2026 -0500

    Fix NullPointerException in StopWatch.getStopTime()
---
 src/changes/changes.xml                            |  1 +
 .../org/apache/commons/lang3/time/StopWatch.java   |  3 ++-
 .../apache/commons/lang3/time/StopWatchTest.java   | 22 ++++++++++++++--------
 3 files changed, 17 insertions(+), 9 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 34421ae71..a789119ab 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -129,6 +129,7 @@ The <action> type attribute can be add,update,fix,remove.
     <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Add ArrayUtils.getDimensions(Object).</action>
     <action issue="LANG-1707" type="add" dev="ggregory" due-to="Ivan Malutin, 
Gilles Sadowski, Gary Gregory">Add ArrayUtils.concat() methods for 
concatenating multiple arrays #1519.</action>
     <action                   type="add" dev="ggregory" due-to="Lars Helge 
Ă˜verland, Gary Gregory">Fix StopWatch's stopInstant match stopTimeNanos when 
split is called #1610.</action>
+    <action                   type="add" dev="ggregory" due-to="Gary 
Gregory">Fix NullPointerException in StopWatch.getStopTime().</action>
     <!-- UPDATE -->
     <action                   type="update" dev="ggregory" due-to="Gary 
Gregory, Dependabot">Bump org.apache.commons:commons-parent from 92 to 97 
#1498.</action>
     <action                   type="update" dev="ggregory" due-to="Gary 
Gregory">[test] Bump org.apache.commons:commons-text from 1.14.0 to 
1.15.0.</action>
diff --git a/src/main/java/org/apache/commons/lang3/time/StopWatch.java 
b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
index 4604a72c0..08d66b122 100644
--- a/src/main/java/org/apache/commons/lang3/time/StopWatch.java
+++ b/src/main/java/org/apache/commons/lang3/time/StopWatch.java
@@ -519,7 +519,8 @@ public Instant getStopInstant() {
     @Deprecated
     public long getStopTime() {
         // stopTimeNanos stores System.nanoTime() for elapsed time
-        return getStopInstant().toEpochMilli();
+        final Instant stop = getStopInstant();
+        return stop != null ? stop.toEpochMilli() : 0;
     }
 
     /**
diff --git a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java 
b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
index 944e1d10d..01abeaf54 100644
--- a/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
+++ b/src/test/java/org/apache/commons/lang3/time/StopWatchTest.java
@@ -393,6 +393,14 @@ void testSplit() throws InterruptedException {
         assertTrue(totalDuration.toMillis() >= sleepMillisX3);
     }
 
+    @Test
+    void testSplitGetStopInstant() {
+      final StopWatch watch = StopWatch.createStarted();
+      watch.split();
+      assertNotNull(watch.getStopTime());
+      assertNotNull(watch.getStopInstant());
+    }
+
     @Test
     void testSplitsWithStringLabels() {
         final StopWatch watch = new StopWatch();
@@ -423,14 +431,6 @@ void testSplitsWithStringLabels() {
         assertThrows(IllegalStateException.class, watch::unsplit);
     }
 
-    @Test
-    void testSplitGetStopInstant() {
-      final StopWatch watch = StopWatch.createStarted();
-      watch.split();
-      assertNotNull(watch.getStopTime());
-      assertNotNull(watch.getStopInstant());
-    }
-
     @Test
     void testSplitWithLabelGetStopInstant() {
       final StopWatch watch = StopWatch.createStarted();
@@ -445,6 +445,12 @@ void testStatic() {
         assertTrue(watch.isStarted());
     }
 
+    @Test
+    void testGetStopTime() throws InterruptedException {
+        final StopWatch watch = StopWatch.createStarted();
+        assertEquals(0, watch.getStopTime());
+    }
+
     @Test
     void testStopInstantSimple() throws InterruptedException {
         final StopWatch watch = StopWatch.createStarted();

Reply via email to