我正在嘗試將圖像保存到我的應用程序中。但是當我嘗試持久化圖像對象時,我得到空指針異常。這裏是我的圖片類:當我嘗試保存圖像時,爲什麼會出現NullPointerException?
@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異常?提前致謝。
那麼'imgObj'不爲空,你在下面說'pm'不爲空,所以你最好打賭是通過'pm.makePersistent()'來查看哪個字段在'imgObj'中導致問題。在這裏添加堆棧跟蹤的相關部分也可能很有用。 – Windle
img.getImageData()爲空。這是我得到的blobkey。爲什麼img.getImageData()爲空?我是不是以正確的方式獲取img? –
Piscean