0

I write project, where I work with Groovy and Java. I have this Groovy script in my project:

int sum(def a, def b) {
return (int)a + (int)b;
}

And in my Main Java class I write this:

public static void main(String[] args) {
    int answer = 0;
    String[] arguments = new String[]{"1", "2"};
    GroovyShell shell = new GroovyShell();
    try {
        answer = (int) shell.run(new File("src/Summary.groovy"), arguments);
        System.out.print(answer);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

But I have NullPointerException in this line: answer = (int) shell.run(new File("src/Summary.groovy"), arguments); So, what I want? I want run Main class and call groovy script, which contain function of sum a + b and return this value to Java code. How can I do it correct? UPD: Full stackTrace from Main class:

Exception in thread "main" java.lang.NullPointerException
at Main.main(Main.java:12)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)

UPD2: Incorrect output: a:1 b:2 answer: 33 I use this script:

def sum (int a, int b) {
print("a:" + a + " b:" + b + "\n")
return a + b
}
return sum (args[0].toInteger(), args[1].toInteger())

And code from Main class correct call it, but answer incorrect

4
  • It would be much easier if you put the full stack trace... It will enable us to find out which part was null in the line... Commented Jul 3, 2015 at 5:23
  • @Codebender I put stack trace from Main class, which start my project Commented Jul 3, 2015 at 5:31
  • @Giymose, so now investigate line no 140. Commented Jul 3, 2015 at 8:19
  • Your groovy script appears to be casting Strings to int. This isn't the problem you're seeing now, but I believe it will be your next problem on the list Commented Jul 3, 2015 at 10:38

1 Answer 1

1

You have to call something in your script - not just provide a function. So your script could look like:

int sum(def a, def b) {
   return ((int)a) + ((int)b)
}
return sum (args[0], args[1])

Still casting a String to an int looks really weird - maybe you wanted to parse the strings to ints or something (e.g. "1".toInteger() or a.toInteger() as in your case).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot. I do it, but result isn't correct. I see: a:1 b:2 answer: 33. see code in top
It works fine on my box - printing out "3". Are you sure you don't print the result out twice? ` a:1 b:2 3 Process finished with exit code 0`

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.