google-app-engine - "message": "java.lang.NullPointerException" when trying to insert entity via api-explorer

标签 google-app-engine google-cloud-datastore google-api-client google-cloud-endpoints

我是 appEngine 的新手,我正在尝试我的项目所需的简单操作: 我创建了简单的图像 JDO 类:

@PersistenceCapable(identityType = IdentityType.APPLICATION)
public class Image {

@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key key;

@Persistent
private Blob image;

@Persistent
private String type;

@Persistent
private String description;

public Key getKey() {
    return key;
}

public void setKey(Key key) {
    this.key = key;
}

public Blob getImage() {
    return image;
}

public void setImage(URL url) throws IOException {

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                url.openStream()));

        String line;
        StringBuffer stbf = new StringBuffer();

        while ((line = reader.readLine()) != null) {
            stbf.append(line);
        }
        image = new Blob(stbf.toString().getBytes());
        reader.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

成功创建并部署到 AppEngine 的端点。 当我尝试通过 google-api-explorer 将简单对象插入到数据存储区时 https://developers.google.com/apis-explorer/?base=https://my-application.appspot.com/_ah/api#s/ , 仅使用 id 和 appID 参数链接到图像和 key ,我收到以下错误:

503 Service Unavailable

- Show headers -

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "backendError",
    "message": "java.lang.NullPointerException"
   }
  ],
  "code": 503,
  "message": "java.lang.NullPointerException"
 }
}

当我将键从 Long 类型更改为 by 时,查询正确执行并且我在数据存储区中看到了新实体。

除此之外,在文档中说“如果编码的键字段为空,则在保存对象时该字段将填充系统生成的键”。但是好像没有key不接受?

谁能帮我解决这个问题?

最佳答案

看看生成的方法containsImage:

标准生成的代码不会检查 getKey() 是否返回 null。您需要包括以下内容:

if (image.getKey() == null) {
            return false;
}

在执行 getObjectById 之前。

关于google-app-engine - "message": "java.lang.NullPointerException" when trying to insert entity via api-explorer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15043532/

相关文章:

python - 谷歌应用引擎和谷歌云 sql 上的 Flask SqlAlchemy

python - Google App Engine python,GQL,仅从数据存储中选择一列

python - 如何更改 NDB 记录的祖先?

node.js - rxjs 订阅返回重复项

java - 使用 Google People API (Java) 检索有关联系人的信息

google-app-engine - 使用大量数据存储写入操作的分析 super 代理

java - 应用引擎 : Having a web module and a endpoint module

google-app-engine - 将十六进制值存储为字节导致丢失

google-app-engine - Google Cloud Datastore 控制台 - 服务不可用 503

php - 清理 Magento Google Api OAuth2 集成