java.lang.NullPointerException:在Java中运行Groovy时无法调用方法

时间:2017-07-05 19:06:41

标签: java groovy

我第一次使用Groovy和Java,并在运行简单的主项目时不断出现异常。

例外:

  

线程中的异常" main" java.lang.NullPointerException:无法在null对象

上调用方法sell()

类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();
}

文件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" 
          }

类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);
}

1 个答案:

答案 0 :(得分:0)

发现问题,TradeDsl买入,卖出,需要退货TradeDsl