From defafe7b316d8d2a9591ea8a465a375eec0c48b2 Mon Sep 17 00:00:00 2001 From: Elliotte Rusty Harold Date: Thu, 23 Jul 2026 10:30:47 +0000 Subject: [PATCH] Fix potential NPE in getJdkHome() and use \n instead of System.lineSeparator() Fixes #168 The getJdkHome() method in SelectJdkToolchainMojo had an unchecked null chain that could throw NullPointerException. Each step in the chain (getModel(), getConfiguration(), getChild(), getValue()) is now guarded against null. Also replaces System.lineSeparator() with \n in the error message for consistency. --- .../toolchain/jdk/SelectJdkToolchainMojo.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojo.java b/src/main/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojo.java index 70a6c2b..c6ba2ee 100644 --- a/src/main/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojo.java +++ b/src/main/java/org/apache/maven/plugins/toolchain/jdk/SelectJdkToolchainMojo.java @@ -216,7 +216,7 @@ private void doExecute() throws MisconfiguredToolchainException, MojoFailureExce if (toolchain == null) { throw new MojoFailureException( "Cannot find matching toolchain definitions for the following toolchain types:" + requirements - + System.lineSeparator() + + "\n" + "Define the required toolchains in your ~/.m2/toolchains.xml file."); } @@ -262,8 +262,17 @@ private boolean matches(String key, String reqVal, String tcVal) { } private String getJdkHome(ToolchainPrivate toolchain) { - return ((Xpp3Dom) toolchain.getModel().getConfiguration()) - .getChild("jdkHome") - .getValue(); + ToolchainModel model = toolchain.getModel(); + if (model == null) { + return null; + } + Object configuration = model.getConfiguration(); + if (configuration instanceof Xpp3Dom) { + Xpp3Dom child = ((Xpp3Dom) configuration).getChild("jdkHome"); + if (child != null) { + return child.getValue(); + } + } + return null; } }