无法实例化类的新实例,没有零参数构造函数,dynamodb查询/扫描

时间:2018-12-11 20:17:06

标签: android android-asynctask amazon-dynamodb dynamodb-queries

我的UserPicture类在以下行出现错误:

List<Picture> scanResult = mapper.scan(Picture.class, scanExpression);

错误:

  

java.lang.InstantiationException:java.lang.Class   com.stylesapp.stylesapp.DynamoDBController $ Picture没有零   参数构造器

  

com.amazonaws.mobileconnectors.dynamodbv2.dynamodbmapper.DynamoDBMappingException:   无法实例化类的新实例

我的Picture类和UserPicture类都在下一个类中。为什么会出现这些错误?为了解决此问题,我所做的一些事情是将UserPicture类更改为static,并实现不带参数的默认构造函数。

异步类

public class UserPicture extends AsyncTask<String, Void, List<Map<String,String>>> {

    String tag = "USERPICTURE controller ";
    @Override
    protected List<Map<String,String>> doInBackground(String... params) {

        Log.d(tag, "Fetching pictures");

        Context context = AndroidApplication.getAppContext();

        CognitoCachingCredentialsProvider credentials = new CognitoCachingCredentialsProvider(
                context, Constants.ACCOUNT_ID, Constants.IDENTITY_POOL_ID,
                Constants.UNAUTH_ROLE_ARN, null, Regions.US_EAST_2);
        AmazonDynamoDBClient ddb = new AmazonDynamoDBClient(credentials);
        ddb.setRegion(Region.getRegion(Regions.US_EAST_2));
        DynamoDBMapper mapper = DynamoDBMapper.builder().dynamoDBClient(ddb).build();

        Log.d(tag,"params sub obtained - " + params[0]);

        Condition condition = new Condition().withComparisonOperator(
                ComparisonOperator.EQ.toString()).withAttributeValueList(
                new AttributeValue().withS(params[0]));
        DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();
        scanExpression.addFilterCondition("sub", condition);

        Picture pic = new Picture();

        List<Picture> scanResult = mapper.scan(Picture.class, scanExpression);
        List<Map<String,String>> picList = new LinkedList<Map<String,String>>();
        //Log.d(tag, "scanResult = " +scanResult);
        String name;
        for (Picture picture: scanResult) {
            Map<String, String> tmp = new HashMap<String, String>();
            tmp.put("id", picture.getId());
            name = picture.getId() + picture.getExtension();
            tmp.put("name", name);
            tmp.put("date", picture.getDateCreated());

            // Add to the list
            picList.add(tmp);
        }
        Log.d(tag,"pickList.get(0).get(name)" + picList.get(0).get("name"));

        return picList;
    }
    @Override
    protected void onPostExecute(List result) { }
    @Override
    protected void onPreExecute() {}
    @Override
    protected void onProgressUpdate(Void... values) {}
}

图片课

@DynamoDBTable(tableName = Constants.PICTURE_TABLE)
public class Picture {

    //public Picture(){}

    final String TAG = "PictureTable";
    private String id;
    private String sub;
    Date d;
    DateFormat f;
    private String date_created;
    private final String dateTimeStringFormat = "MM/dd/yyyy HH:mm:ss";
    private String extension;

    // Default constructor
    //public Picture(){}

    // Id primary key
    @DynamoDBHashKey(attributeName = "id")
    @DynamoDBAutoGeneratedKey
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }

    @DynamoDBAttribute(attributeName = "sub")
    public String getSub() { return this.sub; }
    public void setSub(String sub) { this.sub = sub; }


    @DynamoDBAttribute(attributeName = "date_created")
    public String getDateCreated() { return this.date_created; }
    public void setDateCreated() {
        DateFormat f = new SimpleDateFormat(dateTimeStringFormat);
        this.d = Calendar.getInstance().getTime();
        this.date_created = f.format(this.d);
    }

    @DynamoDBAttribute(attributeName = "extension")
    public String getExtension(){return this.extension;}
    public void setExtension(String extension){this.extension = extension;}

}

0 个答案:

没有答案