【问题标题】:"message": "java.lang.NullPointerException" when trying to insert entity via api-explorer“消息”:尝试通过 api-explorer 插入实体时出现“java.lang.NullPointerException”
【发布时间】:2013-02-23 17:42:06
【问题描述】:

我是 appEngine 的新手,我正在尝试我的项目所需的简单事情: 我创建了简单的 JDO Image 类:

@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 参数的图像和密钥,我就会收到以下错误:

503 Service Unavailable

- Show headers -

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

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

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

有人可以帮我解决这个问题吗?

【问题讨论】:

  • 我认为您正在使用 Endpoints,因为您通过资源管理器访问您的页面?你能发布你的 JDO 类的其余部分,以及你的 Endpoint 类的代码吗?
  • 这是整个jdo类。
  • 那么你能发布 Endpoints 类吗?这是带有@Api 注释的类。

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


【解决方案1】:

看看生成的方法 containsImage:

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

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

在它执行 getObjectById 之前。

【讨论】:

  • 现在看来这个bug已经修复了,生成的代码也包含了这个测试。
  • 尚未修复。今天为 Eclipse 更新了整个 appengine 堆栈。问题依然存在。
【解决方案2】:

我一直在尝试解决这个问题。建议的答案是正确的。我想补充一点:这在 GAE SDK 或 Eclipse 插件中没有固定。 IE。仍然没有检查空键。谢谢你的提示!抱歉,我无法添加评论,不知道为什么。

【讨论】:

    猜你喜欢
    • 2019-12-03
    • 1970-01-01
    • 2019-12-30
    • 2014-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-21
    • 1970-01-01
    相关资源
    最近更新 更多