title: java.lang.NullPointerException: Cannot invoke method when running Groovy in Java
body: <p>I am using Groovy and Java for the first time and keep getting exception while running my simple main project.</p>
  
  <h2>Exception:</h2>
  
  <blockquote>
    <p>Exception in thread "main" java.lang.NullPointerException: Cannot invoke method sell() on null object</p>
  </blockquote>
  
  <h2>class App Java:</h2>
  
  <pre><code>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();
  }
  </code></pre>
  
  <h2>file Groovy:</h2>
  
  <pre><code> 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" 
            }
  </code></pre>
  
  <h2>class TradeDsl:</h2>
  
  <pre><code>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);
  }
  </code></pre>