title: Jython - Class Cast Exception with PyObject
body: <p>I am trying to create and cast an object in Jython and I am receiving the following error:</p>
  
  <pre><code>Exception in thread "MainThread" java.lang.ClassCastException: org.python.core.PySingleton cannot be cast to resources.ixia.IxNetType
  at resources.ixia.IxNetFactory.create(IxNetFactory.java:34)
  at resources.ixia.IxiaTest.run(IxiaTest.java:34)
  at resources.ixia.IxiaTest.&lt;init&gt;(IxiaTest.java:14)
  at resources.ixia.IxiaTest.main(IxiaTest.java:42)
  </code></pre>
  
  <p>Here is the code:</p>
  
  <pre><code>import org.python.core.PyObject;
  import org.python.util.PythonInterpreter;
  
  public class IxNetFactory {
  
      private PyObject ixNetClass;
      private PythonInterpreter interpreter;
  
      public IxNetFactory(String script_dir) {
          script_dir=script_dir.replace("\\", "/");
  
          interpreter = new PythonInterpreter();
  
          interpreter.exec("import sys");             
          interpreter.exec("sys.path.append('"+script_dir+"')");
          interpreter.exec("import time");
          interpreter.exec("import os");
          interpreter.exec("from ixnetworks import IxNet");
          //interpreter.exec("from utils import sm");
          //interpreter.exec("from utils import cpf");
  
          ixNetClass = interpreter.get("IxNet");
      }
  
      /*
       * Create an IxNet object
       * 
       * Usage: ixNet.create();
       */
      public IxNetType create() {
          PyObject ixNetObject = ixNetClass.__call__();
          return (IxNetType)ixNetObject.__tojava__(IxNetType.class);
      }
  
      public void close() {
          interpreter.close();
      }
  }
  </code></pre>
  
  <p>For the life of me I cannot figure out what I am doing wrong. From all of the things I have read, I seem to be doing this correctly but I cannot get it to work.</p>
  
  <p>If anyone with experience with Jython could tell me what I am doing wrong it would be much appreciated.</p>