Yonatan Levin
Yonatan Levin

Reputation: 372

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

I`m new in appEngine and i trying simple things that are necessary for my project: I create simple JDO class of 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();
    }

}

End-Points where succesfully created and deplyoed to AppEngine. When i trying to insert simple object to datastore via google-api-explorer https://developers.google.com/apis-explorer/?base=https://my-application.appspot.com/_ah/api#s/, with just link to image and Key with id and appID paraemeters, i recieve the following error:

503 Service Unavailable

- Show headers -

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

When i changing key to by from type Long, query executed properly and i see new entity in datastore.

Additional to that, in Documentation said that "If the encoded key field is null, the field is populated with a system-generated key when the object is saved". But seems that it`s not accept it without key?

Can anybody help me with this issue?

Upvotes: 2

Views: 2388

Answers (2)

D2TheC
D2TheC

Reputation: 2369

I've been trying to fix this for a while. The suggested answer is correct. I thought to add a note: this is NOT fixed in the GAE SDK or Eclipse plugin. I.e. null keys are still not checked. Thanks for the tip! Sorry I could not add a comment, don't know why.

Upvotes: 0

Richard Russell
Richard Russell

Reputation: 1125

Have a look at the generated method containsImage:

The standard generated code doesn't check if getKey() returns null. You'll need to include something like:

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

before it does a getObjectById.

Upvotes: 6

Related Questions