Note that there are some explanatory texts on larger screens.

plurals
  1. PONullpointerexception throws when inserting entity using Auto-generated Classendpoint insert method
    primarykey
    data
    text
    <p>I am confused to using auto-generated endpoint class. I want to use generated endpoint to insert new object into datastore. But, an exception is throwing. </p> <pre><code>fooEndpoint.insertFoo(foo); // throws null pointer exception </code></pre> <p>My entity class is similar with the given example at this source: <a href="https://developers.google.com/appengine/docs/java/datastore/jpa/overview." rel="noreferrer">https://developers.google.com/appengine/docs/java/datastore/jpa/overview.</a></p> <p>Here is my entity:</p> <pre><code>@Entity public class Foo { @Id @GeneratedValue(strategy=GenerationType.IDENTITY) private Key ID; </code></pre> <p>Here is the stack trace:</p> <pre><code>java.lang.NullPointerException at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:318) at org.datanucleus.api.jpa.JPAEntityManager.find(JPAEntityManager.java:256) at com.FooEndpoint.containsFoo(FooEndpoint.java:150) at com.FooEndpoint.insertFoo(FooEndpoint.java:96) </code></pre> <p>On the other side, I can insert new object when I use the EntityManager persist method. Because, this does not check exist or not on the datastore.</p> <p>I expect that, classEndpoint insert method should save the object and assing auto key to ID field. </p> <p>Or I need to initialize the ID field. </p> <p>Here is auto-generated endpoint class insertFoo method.</p> <pre><code> /** * This inserts a new entity into App Engine datastore. If the entity already * exists in the datastore, an exception is thrown. * It uses HTTP POST method. * * @param foo the entity to be inserted. * @return The inserted entity. */ public Foo insertFoo(Foo foo) { EntityManager mgr = getEntityManager(); try { if (containsFoo(foo)) { throw new EntityExistsException("Object already exists"); } mgr.persist(foo); } finally { mgr.close(); } return foo; } </code></pre> <p>Here is the containsFoo method </p> <pre><code> private boolean containsFoo(Foo foo) { EntityManager mgr = getEntityManager(); boolean contains = true; try { Foo item = mgr.find(Foo.class, foo.getID()); // exception occurs here if (item == null) { contains = false; } } finally { mgr.close(); } return contains; } </code></pre> <p>foo.getID() is null. Because, it is new object. I am expecting that, app engine creates a key for it. Or I need to explicitly create a key for it?</p> <p>Other fields in Foo class are simple types such as String and booleans. </p> <p>Thanks for your time. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload