java DynamoDBMapper - 无法实例化类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16246526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 22:22:45  来源:igfitidea点击:

DynamoDBMapper - Failed to instantiate class

javaexceptionamazon-dynamodb

提问by DGolberg

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:

我在 Amazon 的 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; }
}

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?

我基本上只是尝试执行 GetItemRequest 然后绘制特定属性,但我收到指向 line 的错误PStatus data = mapper.load(PStatus.class, "onFarm", new Integer(1655));。我到底做错了什么?

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)

采纳答案by DGolberg

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!

刚刚想出答案。这不是我想的那样,但结果是我在错误的位置上课。他们展示它的方式,我的印象是它是一个子类,但 PStatus 需要是它自己的类、文件等等。我试图在 TestGet 内部对它进行子类化,但它不喜欢那样。猜猜这更像是我遇到的 Java 问题而不是 DBMapper。仍在学习!

回答by some some

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

添加空构造函数,以便调用 java.lang.Class.newInstance0(Unknown Source) 将起作用:

public PStatus()
{

}

回答by Aliza

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

我遇到了同样的问题,并通过删除正在初始化的类的所有构造函数来解决它。

回答by Sahil Singla

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.

您也可能使用了两个 DynamoDBVersionAttribute。它不会发生在您的用例中,但为此也会出现此错误。