Thursday, 3 October 2013

JDO mapped by: NullPointerException when I tried to add object into a list of another class before makePersistent

JDO mapped by: NullPointerException when I tried to add object into a list
of another class before makePersistent

I have File and TaggedDetail classes. One-to-many relationship
File.java
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private BlobKey blobKey;
@Persistent(mappedBy = "file")
private List<TaggedDetail> tags;
TaggedDetail.java
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private File file;
@Persistent
private String tag;
My code for adding tag to file
PersistenceManager pm = PMF.get().getPersistenceManager();
//get file object by blobKey
File file = FileDatastoreUtils.getFileByBlobKey(blobKey);
//create taggedDetail object
TaggedDetail taggedDetail = new TaggedDetail(tag, file);
file.getTags().add(taggedDetail); //this part gave me problem
try {
pm.makePersistent(file);
isSuccess = true;
} finally {
pm.close();
}
The error I got is NullPointerException at file.getTags().add(taggedDetail);
I found out that NullPointerException is thrown because the specified
element is null and this list does not permit null elements.
So I tried "System.out.println(taggedDetail == null);" which gave me
false. That means taggedDetail object is not null.
Did I code the adding tag to file part wrongly? or Is it other problems?

No comments:

Post a Comment