Your code certainly looks okay.  I do not use Eclipse, and I think that perhaps the issue has got something to do with the way in which the IDE is setting up your CLASSPATH.  You should ensure that you have the jython.jar in your CLASSPATH as well as all of the required Java sources.  I do not believe that your "sys.add_package('jyinterface.interfaces','jyinterface.factory', 'org.python.core')" line is needed...it should work without it.  I use Netbeans and I have to ensure that the directory containing my Jython sources is contained within my project CLASSPATH along with jython.jar and plyjy.jar if I am using it.  I do this by going into the project properties and manually adding each of these items as compile-time libraries.  You may be seeing similar requirements with Eclipse.

If you wish, download the PlyJy jar file and then put it into your project.  There should be no need to code a factory implementation if you use PlyJy...just use one of the included factory implementations.  You should then be able to use the factory by doing something like the following:

import org.plyjy.factory.PySystemObjectFactory;

 PySystemObjectFactory factory = new PySystemObjectFactory(
                EmployeeType.class, "employee", "EmployeeSep");

        EmployeeType employee = (EmployeeType) factory.createObject("Sandra", "Johnson",1);
        
        // use employee object


Hope that this works out for you.  Let me know if you have any questions with it.

Best

Josh Juneau
juneau001@gmail.com
http://jj-blogger.blogspot.com
http://www.jythonpodcast.com
Twitter ID:  javajuneau


On Tue, Jan 12, 2010 at 3:51 AM, Sandra Johnson <sand.johnson@gmail.com> wrote:
Thanks Josh. Much appreciated.

I will definitely look at PlyJy and the online jython book.
I have copied the factory code below as well as the python code. They are just the examples that you and Charlie posted in newsletters in 2006.

In Eclipse I have two projects. A PyDev project with EmployeeOct.py and EmployeeSep.py in it and then the java project with the java code for EmployeeFactory, EmployeeType, MainOct and MainSep.

I am also interested in the PyMC package for MCMC simulations. Would it be possible for me to write code that would port the functionality to Jython? I have written a Java gui and want to perform some MCMC simulations based on user information entered, store some results and then interface to a bayesian network. That is the plan.....!

----------------------------------------------
package jyinterface.factory;

import jyinterface.interfaces.EmployeeType;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;

public class EmployeeFactory {
   
    private PyObject jyEmployeeClass;
   
    public EmployeeFactory () {
        PythonInterpreter interpreter = new PythonInterpreter();
        interpreter.exec("from EmployeeOct import EmployeeOct");
        jyEmployeeClass = interpreter.get("EmployeeOct");
    }
   
    public EmployeeType create(String first, String last, String id) {
        PyObject employeeObj = jyEmployeeClass.__call__(new PyString(first),
                                                        new PyString(last),
                                                        new PyString(id));
        return (EmployeeType)employeeObj.__tojava__(EmployeeType.class);
    }
}

------------------------------------------------

import sys
sys.add_package('jyinterface.interfaces','jyinterface.factory', 'org.python.core')

from jyinterface.interfaces import EmployeeType
class EmployeeOct(EmployeeType):
    '''
    classdocs
    '''   
    def __init__(self, first, last, id):
        '''
        Constructor
        '''
        self.first = first
        self.last = last
        self.id = id
       
    def getEmployeeFirst(self):
        return self.first
   
    def getEmployeeLast(self):
        return self.last
   
    def getEmployeeId(self):
        return self.id
-----------------------

Thanks again.

Cheers,
Sandra


On Tue, Jan 12, 2010 at 2:41 PM, Josh Juneau <juneau001@gmail.com> wrote:
All you really need to ensure is that the Jython object that you wish to use from your java code is implementing a java interface.  The factory should then be able to coerce the jython object. If you could send your factory code to the list then I'd be more than happy to take a look at it.

On another note, you may be interested in taking a look at the PlyJy project which resides on Kenai. Jim Baker and myself have contributed a couple of different object factory implementations that you can use there. I also suggest taking a look at the online jython book located at jythonbook.com. We've written a chapter for that book which is all about using java and jython together.

Hope this helps. As I had mentioned, please feel free to send your code and I'd gladly have a look.

Best

Josh Juneau
Application Developer



On Jan 11, 2010, at 5:30 PM, Sandra Johnson <sand.johnson@gmail.com> wrote:

Dear All,

I am new to Jython and decided to try the code supplied in the monthly Newsletters of September and October 2006 as I want to be able to write some object factories for Python classes.

I am coding and testing in Eclipse vsn 3.5.1 with PyDev plugin installed,  Jython 2.5.1 and JRE 6

I copied the code from the newsletters and changed the classes that differed between the two newsletters to have either Sep or Oct in them to distinguish where they came from. I have spent ages trying to find the problem and getting nowhere. One suggestion in an earlier post was to include into the python class

import sys
sys.add_package(' java package' ) - so I added this code as well for the java package 'jyinterface.interfaces'

but to no avail, same NullPointerException & ExceptionInInitializerError.

The only differences between my code and the code provided by Charlie Groves and Josh Juneau are java class name changes (MainSep.java, MainOct.java in package jyinterface.interfaces, EmployeeOct.py and EmployeeSep.py in the Jython project and I put MainSep.java not in package jyinterface as Josh had, but in the jyinterface.interfaces package). I also updated the references to these classes.

Here is the console output I get. Happy to supply the code if anyone feels kind enough to have a look!

Exception in thread "main" java.lang.ExceptionInInitializerError
   at org.python.core.PyObject.<clinit>(PyObject.java:26)
   at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:72)
   at org.python.util.PythonInterpreter.<init>(PythonInterpreter.java:60)
   at jyinterface.factory.EmployeeFactory.<init>(EmployeeFactory.java:13)
   at jyinterface.interfaces.MainOct.main(MainOct.java:18)
Caused by: java.lang.NullPointerException
   at org.python.core.PyType.init(PyType.java:458)
   at org.python.core.PyType.createType(PyType.java:1175)
   at org.python.core.PyType.addFromClass(PyType.java:1116)
   at org.python.core.PyType.fromClass(PyType.java:1182)
   at org.python.core.PyType.<clinit>(PyType.java:30)
   ... 5 more

The lines that are being referred to in the error log are

MainOct.java:18
       EmployeeFactory factory = new EmployeeFactory();
EmployeeFactory.java:13
       PythonInterpreter interpreter = new PythonInterpreter();
PythonInterpreter.java:60

public PythonInterpreter() {    // line 59
       this(null, null);                   // line 60

PythonInterpreter.java:72
           dict = new PyStringMap();
PyObject.java:26
   public static final PyType TYPE = PyType.fromClass(PyObject.class);


PyType.java:458
           if (!Py.BOOTSTRAP_TYPES.contains(underlying_class)) {
PyType.java:1175
       newtype.init(c,needsInners);
PyType.java:1116
       return createType(c, needsInners);
PyType.java:1182
           addFromClass(PyType.class, null);
PyType.java:30
   public static PyType TYPE = fromClass(PyType.class);

Cheers,
Sandra
------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev
_______________________________________________
Jython-users mailing list
Jython-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jython-users



--
Sandra
___________________________
Dr Sandra Johnson
School of Mathematical Sciences
Room O311, O Block, Gardens Point
Queensland University of Technology
G.P.O. Box 2434
Brisbane, QLD 4001

t: +61 (0)7 3138 8314
f: +61 (0)7 3138 2310
m: 0416 371091