【问题标题】:Why am i getting NullPointerException when i try to save image?为什么我尝试保存图像时收到 NullPointerException?
【发布时间】:2012-10-23 15:06:03
【问题描述】:

我正在尝试将图像保存在我的应用中。但是当我尝试持久化图像对象时出现空指针异常。这是我的图像类:

 @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() 并查看@ 中的哪个字段987654326@ 导致问题。添加堆栈跟踪的相关部分在这里也可能有用。
  • img.getImageData() 为空。这是我得到的 blobkey 。为什么 img.getImageData() 为空?我没有以正确的方式获取 img 吗?

标签: java google-app-engine persistence blobstore object-persistence


【解决方案1】:

你可以在这条线上有一个 Npe 的唯一方法:

pm.makePersistent(imgObj);

当 pm 为空时

你应该检查那部分:

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

你的 persistence.xml 在类路径中吗?

【讨论】:

  • 我打印了下午,显示为“org.datanucleus.store.appengine.jdo.DatastoreJDOPersistenceManager@1367f48c”。这意味着至少它不为空
  • 我正在使用同一行代码在其他 servlet 中保留大量对象。并且工作正常。我认为它是另一回事。
  • 你能添加你的堆栈跟踪吗? :D
  • 这是我在控制台中得到的:异常错误:java.lang.NullPointerException 没有别的
【解决方案2】:

根据我的经验,当您从 ImagesServiceFactory.makeImageFromBlob 获取图像而不对其应用任何转换时,img.getImageData() 会返回 null。如果您想在不进行任何图像转换的情况下从 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 2020-06-29
    • 1970-01-01
    • 1970-01-01
    • 2015-11-22
    相关资源
    最近更新 更多