It looks like you're retrieving an existing object from the datastore,
adding a tag, and then re-persisting. Does this Set have any items in it
before you call add? Even if it's instantiated before you persist the
original entity, App Engine's JDO/JPA layer will return null for empty
lists, so this could be your issue. Just change your addNewTag method
slightly:

public void addNewTag(Key tagKey) {
  if (selectedTags == null) {
    selectedTags = new HashSet<Key>();
  }

  selectedTags.add(tagKey);
}

- Jason

On Sun, Nov 1, 2009 at 1:43 PM, dataStorm <[email protected]> wrote:

>
> Hello, can someone help me with this, I am trying to follow this post
> http://code.google.com/appengine/docs/java/datastore/relationships.html
> to
> create relationship between LINKS and TAGS.
>
> I am getting an error:
>
> Caused by:
>
> java.lang.NullPointerException
>        at com.linkytty.Linkstore.addNewTag(Linkstore.java:101)
>        at com.linkytty.TagLinker.doPost(TagLinker.java:52)
>
>
> This happens when I try to add a key using ".add()"  :
>
> public void addNewTag(Key tagKey) {
>
> selectedTags.add(tagKey);
>
> }
>
>
> addNewTag() is called from my
>
> PersistenceManager pm = PMF.get().getPersistenceManager();
> Linkstore linkObject = pm.getObjectById(Linkstore.class, objectKey);
> linkObject.addNewTag(objectKey);
>
> I've logged my "objectKey" and etc. and it seems that it gets those
> right
> (
> String linkKeyId = req.getParameter("key");
> linkKeyID : aghsaW5reXR0eXIPCxIJTGlua3N0b3JlGAUM
>
> Key objectKey = KeyFactory.stringToKey(linkKeyId);
> objectKey: Linkstore(5)
> )
>
> My Classes:
>
> the class that creates links:
>
>
> public class Linkstore {
>
>        @PrimaryKey
>        @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
>         private Key linkid;
>
>        @Persistent(defaultFetchGroup = "true")
>        private Set<Key> selectedTags;
>
>
> //STUFF HERE
>
>        public Linkstore(User author, String linkcontent, String
> linkcomment,
> Date
> date) {
>                this.author = author;
>                this.linkcontent = linkcontent;
>                this.linkcomment = linkcomment;
>
>                this.date = date;
>
>                selectedTags = new HashSet<Key>();
>
>        }
>
>        public Key getId() {
>
>                return linkid;
>
>        }
>
>        public String getKeyAsString() {
>                if (linkid == null)
>                        return null;
>                return KeyFactory.keyToString(linkid);
>        }
>
>        // STUFF HERE
>
>
>        public void addNewTag(Key tagKey) {
>
>                        selectedTags.add(tagKey);
>
>        }
>
> }
>
> The class that captures the post:
>
>
>        public void doPost(HttpServletRequest req, HttpServletResponse resp)
>                        throws IOException {
>
>                String linkKeyId = req.getParameter("key");
>                Key objectKey = KeyFactory.stringToKey(linkKeyId);
>
>                String tagStringKey = req.getParameter("tagkey");
>                Key tagObjectKey = KeyFactory.stringToKey(tagStringKey);
>
>                PersistenceManager pm = PMF.get().getPersistenceManager();
>                Linkstore linkObject = pm.getObjectById(Linkstore.class,
> objectKey);
>
>                linkObject.addNewTag(objectKey);
>
>                resp.sendRedirect("/");
>        }
>
>
> ///
> Does the "@Persistent(defaultFetchGroup = "true")" make any
> difference?
>
>
> Help & Thanks!
>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Google App Engine for Java" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/google-appengine-java?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to