Unable to invoke no-args constructor for Product.class

爱⌒轻易说出口 提交于 2019-12-20 01:54:26

问题


I am Using GSON and Volley library for networking in my android application but while converting the Json response to Model classes using Gson i am getitng the following error:

88-2006/ E/Volley﹕ [121] NetworkDispatcher.run: Unhandled exception java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.
    java.lang.RuntimeException: Unable to invoke no-args constructor for class [Lcom.example.model.Product;. Register an InstanceCreator with Gson for this type may fix this problem.

these are my POJO classes i am using : Product.java

public class Product {

    private String status;

    private List<Result> results = new ArrayList<Result>();

    private Pagination pagination;
    public Product(){}

    // getters and setters
}

Pagination.java

public class Pagination {

    private String first;

    private String previous;

    private String next;

    private String last;
    public Pagination(){}
}

Result.java

public class Result {

    private String id;

    private Properties properties;
    public Result{}

}

Properties.java

public class Properties {

    private String qbcode;

    private String name;

    private String purchasedate;

    private String vendor;

    private String thumbnail;
    public Properties(){}
}

I have gone through the existing questions which are same like this , as per answers i have found i added the no arg constructor to all the classes but still i am getting the error please help me in solving this issue

The Json String:

{
  "status" : "OK",
  "results" : [ {
    "id" : "IzIzOjE=",
    "properties" : {
      "qbcode" : "IN-1-1",
      "name" : "Test Name",
      "purchasedate" : "2015-05-21",
      "vendor" : "Test Vendor",
      "thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
    }
  }, {
    "id" : "IzIzOjI=",
    "properties" : {
      "qbcode" : "IN-1-2",
      "name" : "Test Name",
      "purchasedate" : "2015-05-21",
      "vendor" : "Test Vendor",
      "thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
    }
  }, {
    "id" : "IzIzOjM=",
    "properties" : {
      "qbcode" : "IN-1-3",
      "name" : "Test Name",
      "purchasedate" : "2015-05-21",
      "vendor" : "Test Vendor",
      "thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
    }
  },{
    "id" : "IzIzOjU=",
    "properties" : {
      "qbcode" : "IN-1-5",
      "name" : "Test Name",
      "purchasedate" : "2015-05-21",
      "vendor" : "Test Vendor",
      "thumbnail" : "http://en.wikipedia.org/static/images/project-logos/enwiki.png"
    }
  } ],
  "pagination" : {
    "first" : "/list?size=20",
    "previous" : "/list?start=IzIzOjE=&size=20",
    "next" : "/list?start=IzIzOjQx&size=20",
    "last" : "/list?start=IzIzOjYx&size=20"
  }
}

回答1:


Add default constructor for all classes. Example:

public class Product{

    public Product(){
    }

}



回答2:


problem solved, I am doing a foolish call To Gson because i am obtaining a Product class that contains list of other Classes(Result.class etc) but i am sending Product[].class to Gson to convert Hence it thrown exception.




回答3:


Your model having private attribute you must create setter for the same or create constructor with all parameter like below:

public class Product {

    private String status;
    private List<Result> results = new ArrayList<Result>();
    private Pagination pagination;

    public void setStatus(String status) {
        this.status = status;
    }

    public void setResults(List<Result> results) {
        this.results = results;
    }

    public void setPagination(Pagination pagination) {
        this.pagination = pagination;
    }
}

or

public class Product {

    private String status;
    private List<Result> results = new ArrayList<Result>();
    private Pagination pagination;

    public Product(String status, List<Result> results, Pagination pagination) {
        this.status = status;
        this.results = results;
        this.pagination = pagination;
    }

}



回答4:


Just add an no argument constructor for the Bean class.

public class Product{

  public Tweet(){
  }

}


来源:https://stackoverflow.com/questions/30371096/unable-to-invoke-no-args-constructor-for-product-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!