0

我正在嘗試將圖像保存到我的應用程序中。但是當我嘗試持久化圖像對象時,我得到空指針異常。這裏是我的圖片類:當我嘗試保存圖像時,爲什麼會出現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異常?提前致謝。

+0

那麼'imgObj'不爲空,你在下面說'pm'不爲空,所以你最好打賭是通過'pm.makePersistent()'來查看哪個字段在'imgObj'中導致問題。在這裏添加堆棧跟蹤的相關部分也可能很有用。 – Windle

+0

img.getImageData()爲空。這是我得到的blobkey 。爲什麼img.getImageData()爲空?我是不是以正確的方式獲取img? – Piscean

回答

0

你可以有一個數Npe在此行的唯一方法:

pm.makePersistent(imgObj); 

是下午的時候是空

你應該檢查部分:

PersistenceManager pm = PMF.get().getPersistenceManager(); 

是你的persistence.xml在類路徑中?

+0

我印了下午,這是顯示「org[email protected]1367f48c」。這意味着至少它不爲空 – Piscean

+0

我堅持使用相同的代碼行在其他servlets中的很多對象。它的工作正常。我認爲它是別的。 – Piscean

+0

你可以添加你的堆棧跟蹤? :D – gjambet

0

根據我的經驗,img.getImageData()返回null當你從ImagesServiceFactory.makeImageFromBlob得到的圖像沒有應用任何轉換。如果你想在沒有進行任何圖像轉換的情況下從blob獲取字節,則實際上並不需要圖像服務,只需從blob存儲區獲取數據即可。我試過a blog post這個代碼,效果很好。

public static byte[] readBlobFully(BlobKey blobKey) { 

    BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService(); 
    BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey); 

    if (blobInfo == null) 
     return null; 

    if (blobInfo.getSize() > Integer.MAX_VALUE) 
     throw new RuntimeException("This method can only process blobs up to " + Integer.MAX_VALUE + " bytes"); 

    int blobSize = (int) blobInfo.getSize(); 
    int chunks = (int) Math.ceil(((double) blobSize/BlobstoreService.MAX_BLOB_FETCH_SIZE)); 
    int totalBytesRead = 0; 
    int startPointer = 0; 
    int endPointer; 
    byte[] blobBytes = new byte[blobSize]; 

    for (int i = 0; i < chunks; i++) { 

     endPointer = Math.min(blobSize - 1, startPointer + BlobstoreService.MAX_BLOB_FETCH_SIZE - 1); 

     byte[] bytes = blobstoreService.fetchData(blobKey, startPointer, endPointer); 

     for (int j = 0; j < bytes.length; j++) 
      blobBytes[j + totalBytesRead] = bytes[j]; 

     startPointer = endPointer + 1; 
     totalBytesRead += bytes.length; 
    } 

    return blobBytes; 
} 
相關問題