diff --git a/core/src/main/java/org/jruby/embed/jsr223/JRubyEngine.java b/core/src/main/java/org/jruby/embed/jsr223/JRubyEngine.java index 7fd1518d306..b2d350b6c69 100644 --- a/core/src/main/java/org/jruby/embed/jsr223/JRubyEngine.java +++ b/core/src/main/java/org/jruby/embed/jsr223/JRubyEngine.java @@ -214,7 +214,8 @@ public Object invokeMethod(Object receiver, String method, Object... args) } return container.callMethod(receiver, method, args, Object.class); } catch (Exception e) { - if (e.getCause().getMessage().contains("undefined method")) { + if (e.getCause() != null && e.getCause().getMessage() != null + && e.getCause().getMessage().contains("undefined method")) { throw wrapMethodException(e); } throw wrapException(e); @@ -239,7 +240,8 @@ public Object invokeFunction(String method, Object... args) } return container.callMethod(container.getProvider().getRuntime().getTopSelf(), method, args, Object.class); } catch (Exception e) { - if (e.getCause().getMessage().contains("undefined method")) { + if (e.getCause() != null && e.getCause().getMessage() != null + && e.getCause().getMessage().contains("undefined method")) { throw wrapMethodException(e); } throw wrapException(e); diff --git a/core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java b/core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java index 9b76c00becd..3c2a83d1607 100644 --- a/core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java +++ b/core/src/test/java/org/jruby/embed/jsr223/JRubyEngineTest.java @@ -617,6 +617,21 @@ public void testInvokeMethod() throws Exception { instance.getBindings(ScriptContext.ENGINE_SCOPE).clear(); } + /** + * Test of invokeMethod method throwing an exception with a null message. + */ + @Test + public void testInvokeMethodWithJavaException() throws Exception { + ScriptEngine instance = newScriptEngine(); + instance.eval("def trigger_npe\nraise java.lang.NullPointerException.new\nend"); + try { + Object result = ((Invocable) instance).invokeMethod(Object.class,"trigger_npe", null); + fail("Expected javax.script.ScriptException"); + } catch (javax.script.ScriptException sex) { + // javax.script.ScriptException is expected + } + } + /** * Test of invokeFunction method, of class Jsr223JRubyEngine. */ @@ -643,6 +658,21 @@ public void testInvokeFunction() throws Exception { instance.getBindings(ScriptContext.ENGINE_SCOPE).clear(); } + /** + * Test of invokeFunction method throwing an exception with a null message. + */ + @Test + public void testInvokeFunctionWithJavaException() throws Exception { + ScriptEngine instance = newScriptEngine(); + instance.eval("def trigger_npe\nraise java.lang.NullPointerException.new\nend"); + try { + Object result = ((Invocable) instance).invokeFunction("trigger_npe", null); + fail("Expected javax.script.ScriptException"); + } catch (javax.script.ScriptException sex) { + // javax.script.ScriptException is expected + } + } + /** * Test of getInterface method, of class Jsr223JRubyEngine. */