-1

I am using Groovy and Java for the first time and keep getting exception while running my simple main project.

Exception:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke method sell() on null object

class App Java:

public void start() throws CompilationFailedException, IOException {

    File trades = uploadGroovyScript();

    Binding binding = new Binding();
    GroovyShell shell = new GroovyShell(binding);
    shell.evaluate(trades);
    TradeDsl tradeDsl = new TradeDsl();
    Closure c = (Closure) binding.getVariable("trades1"); 
    c.setDelegate(tradeDsl);
    c.call();
}

file Groovy:

 trades1 = {
            buy 100,200 from "MSFT" 
            buy 1000,200 from "APPL" 
            buy 500,200 from "VMW" 
            sell 50,200 from "MSFT" 
            buy 1200,200 from "MSFT" 
            sell 200,200 from "VMW" 
            buy 1200,200 from "APPL" 
          }

class TradeDsl:

public void buy(int quantity1 , int quantity2) {
    System.out.println("Buying " + quantity1 + " or " + quantity2);
}

public void sell(int quantity1) {
    System.out.println("Selling " + quantity1);
}

public void from(String epic) {
    System.out.println("from " + epic);
}
Ggdw
  • 2,509
  • 5
  • 24
  • 22
  • In your `trades1` file both `buy` and `sell` have two quantities, but your DSL only specifies one quantity for `sell` (but two for `buy`). – dcsohl Jul 05 '17 at 19:31
  • Hi, thanks this is a problem but not the one that caused the NPE, the problem was that the TradeDsl buy, sell, from need to return TradeDsl – Ggdw Jul 05 '17 at 19:37

1 Answers1

0

found the problem, the TradeDsl buy, sell, from need to return TradeDsl

Ggdw
  • 2,509
  • 5
  • 24
  • 22