title: Ruby Scripting System sending NullPointerException for Java
body: <p>I want to script content using Ruby on a Java application I am currently working on, but unfortunately I keep getting NullPointerException's with the system. It's suppose to load a series of Ruby scripts to be executed within Java.</p>
  
  <pre><code>Exception in thread "main" java.lang.NullPointerException
      at com.anamoly.utilities.PluginInterpreter.executePlugin(PluginInterpreter.java:60)
      at com.anamoly.Launcher.main(Launcher.java:13)
      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:483)
      at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
  </code></pre>
  
  <p>Launcher.java</p>
  
  <pre><code>package com.anamoly;
  
  import com.anamoly.utilities.PluginInterpreter;
  
  /**
   * @author Braeden Dempsey &lt;https://github.com/bdemps98&gt;
   */
  public class Launcher {
      private static PluginInterpreter interpreter = new PluginInterpreter();
  
      public static void main(String[] args) {
          interpreter.loadAllPlugins();
          interpreter.executePlugin("example_function");
      }
  }
  </code></pre>
  
  <p>PluginInterpreter.java</p>
  
  <pre><code>package com.anamoly.utilities;
  
  import java.io.File;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.InputStreamReader;
  import java.util.ArrayList;
  
  import javax.script.Invocable;
  import javax.script.ScriptEngine;
  import javax.script.ScriptEngineManager;
  import javax.script.ScriptException;
  
  /**
   * @author Braeden Dempsey &lt;https://github.com/bdemps98&gt;
   */
  public class PluginInterpreter {
      private ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
      private ScriptEngine scriptEngine = scriptEngineManager.getEngineByName("ruby");
  
      private static final String PLUGIN_DIRECTORY = "/data/plugins/";
  
      public void loadAllPlugins() {
          ArrayList&lt;String&gt; pathCollection = new ArrayList&lt;&gt;();
          String[] path = { "example", "player" };
  
          for (String i : path) {
              pathCollection.add(PLUGIN_DIRECTORY + i + "/");
          }
  
          for (String i : pathCollection) {
              loadPlugin(i);
          }
      }
  
      private void loadPlugin(String directory) {
          File file = new File(directory);
  
          if (file.exists() &amp;&amp; file.isDirectory()) {
              File[] children = file.listFiles();
  
              if (children != null) {
                  for (File child : children) {
                      if (child.isFile() &amp;&amp; child.getName().endsWith(".rb")) {
                          try {
                              scriptEngine.eval(new InputStreamReader(new FileInputStream(child)));
                          } catch (FileNotFoundException | ScriptException exception) {
                              exception.printStackTrace();
                          }
                      } else if (child.isDirectory()) {
                          loadAllPlugins();
                      }
                  }
              }
          }
      }
  
      public void executePlugin(String function, Object... objects) {
          Invocable invocable = (Invocable) scriptEngine;
  
          try {
              invocable.invokeFunction(function, objects);
          } catch (NoSuchMethodException | ScriptException exception) {
              exception.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>example_plugin.rb</p>
  
  <pre><code>class ExamplePlugin
    def example_function
      puts("Launching Anamoly! Sent from Ruby!")
    end
  end
  </code></pre>