我正在尝试将图像保存在我的应用程序中。但是当我尝试持久化图像对象时出现空指针异常。这是我的图像类:
@PersistenceCapable
public class ImageObject {
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;
@Persistent
private String title;
@Persistent
private Blob image;
public ImageObject(){}
// all getters and setters
}
以下是我的 servlet 代码,它给出了异常:
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
PersistenceManager pm = PMF.get().getPersistenceManager();
Map<String, List<BlobKey>> blobs = blobstoreService.getUploads(req);
List<BlobKey> blobKeyList = blobs.get("upload_img_form_input");
BlobKey blobKey = blobKeyList.get(0);
String keyString = req.getParameter("upload_img_form_key");
Key key = KeyFactory.createKey(ImageObject.class.getSimpleName(), keyString);
Image img = ImagesServiceFactory.makeImageFromBlob(blobKey);
ImageObject imgObj = new ImageObject();
imgObj.setKey(key);
imgObj.setTitle(keyString);
imgObj.setImage(img.getImageData());
// i am getting exception at this line where i am trying to persist
pm.makePersistent(imgObj);
谁能告诉我为什么我会得到这个 NullPointerException?提前致谢。