FAQ
Hi!



I am new to Cayenne, and am having some problems. I have been able to
populate some DB records, but when I try to set up a relationship, I get
an NPE on the commit. I am using Cayenne 2.0.2.



Here is the essential code (with some names changed to protect the
innocent):



MyType type = (MyType) context.newObject(MyType.class);

type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
MyType.MYTYPE_ID_PK_COLUMN, 1));

type.setName("MyName");

MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
MyCategory.class, 1);

type.setMyCategory(category);

context.commitChanges();



The MyType table has fields mytype_id (the primary key), name, and a
mycategory_id (foreign key to the mycategory table). I am creating a
new "MyType" object with a mytype_id of 1, name of "MyName", and
pointing to a Category object, that is already in the database, with a
primary key of 1.



When commitChanges() runs, I get:



Exception in thread "main" java.lang.NullPointerException

at
org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
phDiff.java:184)

at
org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
ff.java:53)

at
org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)

at
org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
8)

at
org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
0)

at
com.boxicom.server.db.CreateDb.testStreamTypeAdd(CreateDb.java:96)

at com.boxicom.server.db.CreateDb.main(CreateDb.java:105)



Can anyone help me determine what is going wrong?



Thanks much!

Eric

Search Discussions

  • Mike Kienenberger at Mar 7, 2007 at 4:10 pm
    Eric,

    A good first step would be to get the Cayenne 2.0.2 source, and post
    the relevent line numbers. Maybe it'll be obvious once we see what's
    null.
    On 3/6/07, Eric Floehr wrote:
    Hi!



    I am new to Cayenne, and am having some problems. I have been able to
    populate some DB records, but when I try to set up a relationship, I get
    an NPE on the commit. I am using Cayenne 2.0.2.



    Here is the essential code (with some names changed to protect the
    innocent):



    MyType type = (MyType) context.newObject(MyType.class);

    type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
    MyType.MYTYPE_ID_PK_COLUMN, 1));

    type.setName("MyName");

    MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
    MyCategory.class, 1);

    type.setMyCategory(category);

    context.commitChanges();



    The MyType table has fields mytype_id (the primary key), name, and a
    mycategory_id (foreign key to the mycategory table). I am creating a
    new "MyType" object with a mytype_id of 1, name of "MyName", and
    pointing to a Category object, that is already in the database, with a
    primary key of 1.



    When commitChanges() runs, I get:



    Exception in thread "main" java.lang.NullPointerException

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
    phDiff.java:184)

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
    ff.java:53)

    at
    org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)

    at
    org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
    8)

    at
    org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
    0)

    at
    com.boxicom.server.db.CreateDb.testStreamTypeAdd(CreateDb.java:96)

    at com.boxicom.server.db.CreateDb.main(CreateDb.java:105)



    Can anyone help me determine what is going wrong?



    Thanks much!

    Eric


  • Eric Floehr at Mar 7, 2007 at 6:41 pm
    Hi Mike,

    Thanks...the stack trace I copied with my question had the line numbers,
    I believe. Here they are again for reference:

    Exception in thread "main" java.lang.NullPointerException
    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
    phDiff.java:184)
    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
    ff.java:53)
    at
    org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)
    at
    org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
    8)
    at
    org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
    0)


    Specifically, line 181 in ObjectStoreGraphDiff.java is returning the
    null, which then gets referenced on line 184. Line 181 is:

    Persistent object = (Persistent) objectStore.getNode(id);

    The method is iterating over changes.entrySet(). There are three items
    in the set. The first has an entityName of MyType, the second of
    MyCategory, and the third of MyType. The first two complete fine, it is
    the third that gives the NPE.

    For reference, here is the code that causes the error:

    MyType type = (MyType) context.newObject(MyType.class);
    type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
    MyType.MYTYPE_ID_PK_COLUMN, 1));
    type.setName("MyName");
    MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
    MyCategory.class, 1);
    type.setMyCategory(category);
    context.commitChanges();

    Thanks again for your help,
    Eric


    -----Original Message-----
    From: Mike Kienenberger
    Sent: Wednesday, March 07, 2007 11:10 AM
    To: user@cayenne.apache.org
    Subject: Re: Help: getting NPE at ObjectStoreGraphDiff.preprocess

    Eric,

    A good first step would be to get the Cayenne 2.0.2 source, and post
    the relevent line numbers. Maybe it'll be obvious once we see what's
    null.
    On 3/6/07, Eric Floehr wrote:
    Hi!



    I am new to Cayenne, and am having some problems. I have been able to
    populate some DB records, but when I try to set up a relationship, I get
    an NPE on the commit. I am using Cayenne 2.0.2.



    Here is the essential code (with some names changed to protect the
    innocent):



    MyType type = (MyType) context.newObject(MyType.class);

    type.setObjectId(new ObjectId(MyType.class.getSimpleName(),
    MyType.MYTYPE_ID_PK_COLUMN, 1));

    type.setName("MyName");

    MyCategory category = (MyCategory)DataObjectUtils.objectForPK(context,
    MyCategory.class, 1);

    type.setMyCategory(category);

    context.commitChanges();



    The MyType table has fields mytype_id (the primary key), name, and a
    mycategory_id (foreign key to the mycategory table). I am creating a
    new "MyType" object with a mytype_id of 1, name of "MyName", and
    pointing to a Category object, that is already in the database, with a
    primary key of 1.



    When commitChanges() runs, I get:



    Exception in thread "main" java.lang.NullPointerException

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.preprocess(ObjectStoreGra
    phDiff.java:184)

    at
    org.apache.cayenne.access.ObjectStoreGraphDiff.<init>(ObjectStoreGraphDi
    ff.java:53)

    at
    org.apache.cayenne.access.ObjectStore.getChanges(ObjectStore.java:496)

    at
    org.apache.cayenne.access.DataContext.flushToParent(DataContext.java:120
    8)

    at
    org.apache.cayenne.access.DataContext.commitChanges(DataContext.java:113
    0)

    at
    com.boxicom.server.db.CreateDb.testStreamTypeAdd(CreateDb.java:96)

    at com.boxicom.server.db.CreateDb.main(CreateDb.java:105)



    Can anyone help me determine what is going wrong?



    Thanks much!

    Eric


Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupuser @
categoriescayenne
postedMar 6, '07 at 6:37p
activeMar 7, '07 at 6:41p
posts3
users2
websitecayenne.apache.org

2 users in discussion

Eric Floehr: 2 posts Mike Kienenberger: 1 post

People

Translate

site design / logo © 2022 Grokbase