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 1926e35e3 Fixed NPE getting Stack Trace if Throwable is null (#733)
1926e35e3 is described below

commit 1926e35e3e1e94a43c64cb4d65f51d115fae02cd
Author: Arturo Bernal <[email protected]>
AuthorDate: Sun Apr 3 23:02:00 2022 +0200

    Fixed NPE getting Stack Trace if Throwable is null (#733)
---
 .../java/org/apache/commons/lang3/exception/ExceptionUtils.java   | 8 +++++---
 .../apache/commons/lang3/exception/ContextedExceptionTest.java    | 5 +++++
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git 
a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java 
b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
index e5853294b..bd013a7d6 100644
--- a/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
+++ b/src/main/java/org/apache/commons/lang3/exception/ExceptionUtils.java
@@ -366,14 +366,16 @@ public class ExceptionUtils {
      * On JDK1.3 and earlier, the cause exception will not be shown
      * unless the specified throwable alters printStackTrace.</p>
      *
-     * @param throwable  the {@code Throwable} to be examined
+     * @param throwable  the {@code Throwable} to be examined, may be null
      * @return the stack trace as generated by the exception's
-     *  {@code printStackTrace(PrintWriter)} method
+     * {@code printStackTrace(PrintWriter)} method, or an empty String if 
{@code null} input
      */
     public static String getStackTrace(final Throwable throwable) {
         final StringWriter sw = new StringWriter();
         final PrintWriter pw = new PrintWriter(sw, true);
-        throwable.printStackTrace(pw);
+        if (throwable != null) {
+            throwable.printStackTrace(pw);
+        }
         return sw.getBuffer().toString();
     }
 
diff --git 
a/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java 
b/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java
index 27b70323a..131ec937c 100644
--- 
a/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java
+++ 
b/src/test/java/org/apache/commons/lang3/exception/ContextedExceptionTest.java
@@ -48,6 +48,11 @@ public class ContextedExceptionTest extends 
AbstractExceptionContextTest<Context
         assertTrue(StringUtils.isEmpty(message));
     }
 
+    @Test
+    public void testNullException() {
+        assertEquals("", ExceptionUtils.getStackTrace(null), "Empty 
response.");
+    }
+
     @Test
     public void testContextedExceptionString() {
         exceptionContext = new ContextedException(TEST_MESSAGE);

Reply via email to