NullPointerException in setReverseRelationship() - Solved

From: Craig Jones (crai..ankardsaweigh.com)
Date: Wed Apr 14 2004 - 23:26:18 EDT

  • Next message: sabiha: "relationship between inherited entities"

    [I wanted to reply to an e-mail on cayenne-user from 2 weeks ago. I saw
    it in the archive a few days ago before I subscribed, but for some
    reason today all of the archived messages are missing. That is, right
    now http://objectstyle.org/cayenne/lists.html shows the cayenne-devel
    archives, but not cayenne-user.]

    Anyway, whoever posted the e-mail was getting this:

      java.lang.NullPointerException
       at
    org.objectstyle.cayenne.CayenneDataObject.setReverseRelationship(Cayenne
    DataObject.java:380)
       at
    org.objectstyle.cayenne.CayenneDataObject.setToOneTarget(CayenneDataObje
    ct.java:359)

    I was getting it for a while, too. I finally figured out that my code
    for creating a new object was lacking the registration step. I was
    waiting until just before doing a commit to register the object. I
    thought it was enough for the object to be associated with the data
    context in order to establish relationships between it and other objects
    in the context, but apparently it has to be registered first. So, I
    changed my code from this:

      MyObject myObject = new MyObject();
      myObject.setDataContext(ctxt);
      ...
      myObject.setSomeReference(anotherObject); // blows up
      ...
      ctxt.registerNewObject(myObject);
      ctxt.commitChanges();

    To this:

      MyObject myObject = (MyObject)
    ctxt.createAndRegisterNewObject("MyObject");
      ...
      myObject.setSomeReference(anotherObject);
      ...
      ctxt.commitChanges();

    And now everything works much, much better.

    Perhaps it would help if the setToOneTarget() code somehow guarded
    against the object in question being unregistered?

    // Craig Jones
    Orange, CA



    This archive was generated by hypermail 2.0.0 : Wed Apr 14 2004 - 23:23:40 EDT