Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DynamoDBMapper - Failed to instantiate class

I'm running into an issue of the Java based DynamoDBMapper in Amazon's AWS toolkit throwing the "Failed to instantiate class" exception error. This is my first time attempting to use the DBMapper, so I'm not entirely sure I have everything setup right. My code can be found below:

public static void main(String[] args) {
    dynamoDB = new AmazonDynamoDBClient(credentials);

    DynamoDBMapper mapper = new DynamoDBMapper(dynamoDB);
    PStatus data = mapper.load(PStatus.class, "online", new Integer(1655));
    String assigned = data.getAssigned();
    System.out.println(assigned);
}

@DynamoDBTable(tableName = "projectStatus")
public class PStatus {

    private Integer projID;
    private String status;
    private String assigned;

    @DynamoDBHashKey(attributeName = "status")
    public String getStatus() { return status; }
    public void setStatus(String status) { this.status = status; }

    @DynamoDBRangeKey(attributeName = "projID")
    public Integer getId() { return projID; }
    public void setId(Integer projID) { this.projID = projID; }

    @DynamoDBAttribute(attributeName = "assigned")
    public String getAssigned() { return assigned; }
    public void setAssigned(String assigned) { this.assigned = assigned; }
}

I'm basically just trying to perform a GetItemRequest and then draw out a specific attribute, but I'm getting the error pointing to the line PStatus data = mapper.load(PStatus.class, "onFarm", new Integer(1655));. What exactly am I doing wrong?

Edit: Exact Exception below:

Exception in thread "main" com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMappingException: Failed to instantiate class
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.createKeyObject(DynamoDBMapper.java:325)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:313)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.load(DynamoDBMapper.java:212)
    at test.TestGet.main(TestGet.java:20)
Caused by: java.lang.InstantiationException: test.TestGet$PStatus
    at java.lang.Class.newInstance0(Unknown Source)
    at java.lang.Class.newInstance(Unknown Source)
    at com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper.createKeyObject(DynamoDBMapper.java:323)
like image 794
DGolberg Avatar asked Apr 26 '13 23:04

DGolberg


4 Answers

Add empty constructor so the call java.lang.Class.newInstance0(Unknown Source) will work:

public PStatus()
{

}
like image 67
some some Avatar answered Feb 10 '23 04:02

some some


Just figured out the answer. It wasn't quite what I thought, but it ended up being that I had the class in the wrong location. The way they showed it, I was under the impression that it was a sub-class, but PStatus needed to be its own class, file and all. I was trying to sub-class it inside of TestGet and it wasn't liking that. Guess it was more a Java issue I was having than the DBMapper. Still learning!

like image 39
DGolberg Avatar answered Feb 10 '23 05:02

DGolberg


I had the same issue and solved it by removing all constructors of the class being initialized.

like image 26
Aliza Avatar answered Feb 10 '23 05:02

Aliza


It can also happen that you have used two DynamoDBVersionAttribute. Its not happening in you use-case but for that also can get this error.

like image 41
Sahil Singla Avatar answered Feb 10 '23 05:02

Sahil Singla