Invoking a static Java method in Clojure is smooth; no problem. But when I invoke a non-static method, it throws an error, although I have tried several variations for dot (.) notation, as explained in Clojure Interop documentation.
The Java class:
public class CljHelper {
public void test(String text) {
System.out.println(text);
}
}
The Clojure code:
(ns com.acme.clojure.Play
(:import [com.acme.helpers CljHelper]))
(. CljHelper test "This is a test")
The error:
java.lang.IllegalArgumentException: No matching method: test
This is another attempt, which makes the Java method to be executed but throws an error right after:
(defn add-Id
[x]
(let [c (CljHelper.)]
((.test c x)))) ;;; java.lang.NullPointerException: null in this line
(add-Id "id42")
The error:
java.lang.NullPointerException: null
((.test c x))
- you are calling the result of test, which is nil - parens always have meaning and can not be added to make things more clear like in curly-brace-langs. – cfrick