当我尝试保存图像时为什么会出现NullPointerException?

时间:2021-07-12 23:13:08

I am trying to save image in my app. But i am getting null pointer exception when i try to persist image object. Here is my Image class:

我想在我的应用程序中保存图像。但是当我尝试持久化图像对象时,我得到空指针异常。这是我的Image类:

 @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
 }

Following is my servlet code which is giving exception:

以下是我的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);

Can anybody please tell me why am i getting this NullPointerException? Thanks in advance.

任何人都可以告诉我为什么我得到这个NullPointerException?提前致谢。

2 个解决方案

#1


0  

the only way you could have a Npe on this line :

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

pm.makePersistent(imgObj);

is when pm is null

当pm为null时

you should check that part :

你应该检查那部分:

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

is your persistence.xml in classpath ?

是classpath中的persistence.xml吗?

#2


0  

In my experience, img.getImageData() returns null when you get the image from ImagesServiceFactory.makeImageFromBlob without applying any transformation to it. If you want to get the bytes from the blob without doing any image transformation, you don't actually need the image service and you will just have to fetch the data from the blob store. I tried this code from a blog post and it works well.

根据我的经验,当您从ImagesServiceFactory.makeImageFromBlob获取图像而不对其应用任何转换时,img.getImageData()将返回null。如果你想从blob获取字节而不进行任何图像转换,你实际上并不需要图像服务,你只需要从blob存储中获取数据。我从博客文章中尝试了这个代码,效果很好。

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;
}

#1


0  

the only way you could have a Npe on this line :

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

pm.makePersistent(imgObj);

is when pm is null

当pm为null时

you should check that part :

你应该检查那部分:

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

is your persistence.xml in classpath ?

是classpath中的persistence.xml吗?

#2


0  

In my experience, img.getImageData() returns null when you get the image from ImagesServiceFactory.makeImageFromBlob without applying any transformation to it. If you want to get the bytes from the blob without doing any image transformation, you don't actually need the image service and you will just have to fetch the data from the blob store. I tried this code from a blog post and it works well.

根据我的经验,当您从ImagesServiceFactory.makeImageFromBlob获取图像而不对其应用任何转换时,img.getImageData()将返回null。如果你想从blob获取字节而不进行任何图像转换,你实际上并不需要图像服务,你只需要从blob存储中获取数据。我从博客文章中尝试了这个代码,效果很好。

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;
}