我在亚马逊的AWS工具包中遇到了基于Java的DynamoDBMapper的问题,导致“无法实例化类”异常错误。这是我第一次尝试使用DBMapper,所以我不完全确定我的设置是否正确。我的代码可以在下面找到:
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; }
}
我基本上只是尝试执行GetItemRequest,然后绘制一个特定的属性,但是我收到指向行PStatus data = mapper.load(PStatus.class, "onFarm", new Integer(1655));
的错误。我究竟做错了什么?
编辑:下面的确切异常:
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)
答案 0 :(得分:10)
添加空构造函数,以便调用java.lang.Class.newInstance0(Unknown Source):
public PStatus()
{
}
答案 1 :(得分:3)
刚刚想出答案。这不是我想的那样,但结果是我把课程放在了错误的位置。他们展示它的方式,我的印象是它是一个子类,但PStatus需要成为它自己的类,文件和所有。我试图在TestGet中对它进行子类化,并且不喜欢它。猜猜它比DBMapper更像是一个Java问题。还在学习!
答案 2 :(得分:1)
我遇到了同样的问题,并通过删除正在初始化的类的所有构造函数来解决它。
答案 3 :(得分:0)
使用两个DynamoDBVersionAttribute也可能发生。在您的用例中不会发生这种情况,但是为此也会出现此错误。