title: Unhandled exception type: InstantiationException in processing.org
body: <p>I'm trying to do a simple Class.newInstance() from within processing, but I'm failing :(</p>
  
  <blockquote>
    <p>EDIT: as below instructed, the reason it didn't work was that Processing wraps all
    code into a class making all declared classes internal classes. The java
    language specification states that newInstance() for internal classes
    should be passed a reference to the container class, so after changing
    the call to newInstance to newInstance(this); it worked as intended.</p>
  </blockquote>
  
  <p>code sample below of where it throws an InstantiationException.</p>
  
  <p>Docs said to have a public constructor and that everything would be fine, but alas. </p>
  
  <p>running processing 2.2.1 </p>
  
  <p>Codeflow: it fills up a list of all classes that implement an interface. then we go through state.runnableTypes and try (but fail) to instantiate a new instance of that type. </p>
  
  <p>Please help? </p>
  
  <p>(apologies for the clunky state variable, has to do with an even clunkier ryo unit test system ;))</p>
  
  <pre><code>    State state;
  /* It nicely finds the Wtf class and inserts that into its classes array, 
   * but when i try to instantiate a new one, whatever I try, it throws :(
   */
  void setup() {
    size(1024, 768, P3D);
    noLoop(); 
  //get all the classes
    state = new State();
    Class[] types = this.getClass().getDeclaredClasses();
    state.allTypes = types;
  
    //enumerate castable types
    for (int i = 0, l = types.length; i&lt;l; i++) { 
      Class c = types[i];
      if ( !c.isInterface() &amp;&amp; IMenuRunnable.class.isAssignableFrom(c) ) { 
        println("adding "+c.toString());
        state.runnableTypes.put(c.getSimpleName(), c);
      }
    }
  
    //loop through the list and create some instances //FIXME: Exception handling ;p
    for ( String s : state.runnableTypes.keySet () ) { 
      Class c = state.runnableTypes.get(s);
      IMenuRunnable u = (IMenuRunnable) c.newInstance(); // ERR: calling Class.newInstance() here throws an error??
      java.lang.reflect.Constructor[] ctors = c.getConstructors();
      for ( java.lang.reflect.Constructor ctor : ctors ) { 
        ctor.setAccessible(true);
        Object o = ctor.newInstance(); // ERR: going via constructor array, same error :(
      }
    }
  }
  
  
  
  
  void draw() { 
    background(0);
  }
  
  
  
  
  class State {
    Class[] allTypes;
    HashMap&lt;String, Class&gt; runnableTypes = new HashMap&lt;String, Class&gt;();
  }
  
  
  
  interface IMenuRunnable {
  }
  
  
  
  public class Wtf implements IMenuRunnable { 
    public Wtf() {
    }
  }
  </code></pre>