java - java.lang.NullPointerException:尝试在从服务器到微调器的空对象引用上调用接口方法“int java.util.List.size()”
问题描述
我正在尝试从服务器获取数据到可搜索的微调器但显示 null。我正在使用改造和检查邮递员上的 api 它显示数据但在 android studio 中显示 null。我只显示来自 mysql 数据库的国家名称抱歉英语不好谢谢提前解决我的问题。
public class LabourRegActivity extends AppCompatActivity {
private ArrayList<String> countryname = new ArrayList<String>();
SearchableSpinner countryS,catagoryS,cityS,projectbaseS,district;
EditText name,contact,cnic,perdayrate,address;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_labour_reg);
countryS = findViewById(R.id.country_spinner);
catagoryS = findViewById(R.id.skill_spinner);
cityS = findViewById(R.id.city_spinner);
projectbaseS = findViewById(R.id.projectBase);
district = findViewById(R.id.district_Spinner);
name = findViewById(R.id.Edt_name);
contact = findViewById(R.id.Edt_contact);
cnic = findViewById(R.id.Edt_cnic);
perdayrate = findViewById(R.id.Edt_perdays);
address = findViewById(R.id.Edt_address);
CountryFetchApi fetchApi = ApiClient.getClient().create(CountryFetchApi.class);
// HashMap<String, String> map = new HashMap<>();
Log.d("ok","countryMap"+fetchApi);
Call<List<CountryModel>> model = fetchApi.getcountryrecord();
model.enqueue(new Callback<List<CountryModel>>() {
@Override
public void onResponse(Call<List<CountryModel>> call, Response<List<CountryModel>> response) {
List<CountryModel> data = response.body();
Log.d("ok","country"+response.body());
for (int i = 0; i < data.size(); i++) {
countryname.add(data.get(i).getCountryName());
}
Log.d("leavename", countryS.toString());
adapter = new ArrayAdapter<String>(LabourRegActivity.this, R.layout.support_simple_spinner_dropdown_item,countryname);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countryS.setAdapter(adapter);
}
@Override
public void onFailure(Call<List<CountryModel>> call, Throwable t) {
}
});
}
}
解决方案
public class CountryModel {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("name")
@Expose
private String name;
@SerializedName("skill")
@Expose
private String skill;
@SerializedName("cnic")
@Expose
private Object cnic;
@SerializedName("contact_number")
@Expose
private String contactNumber;
@SerializedName("labour_amount")
@Expose
private String labourAmount;
@SerializedName("perday_project")
@Expose
private Integer perdayProject;
@SerializedName("company_amount")
@Expose
private String companyAmount;
@SerializedName("address")
@Expose
private String address;
@SerializedName("category_name")
@Expose
private String categoryName;
@SerializedName("city_name")
@Expose
private String cityName;
@SerializedName("province")
@Expose
private String province;
@SerializedName("country_name")
@Expose
private String countryName;
@SerializedName("district_name")
@Expose
private String districtName;
public CountryModel() {
this.id = id;
this.name = name;
this.skill = skill;
this.cnic = cnic;
this.contactNumber = contactNumber;
this.labourAmount = labourAmount;
this.perdayProject = perdayProject;
this.companyAmount = companyAmount;
this.address = address;
this.categoryName = categoryName;
this.cityName = cityName;
this.province = province;
this.countryName = countryName;
this.districtName = districtName;
}
@Override
public String toString() {
return getCountryName();
}
public String getId() {
return String.valueOf(id);
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSkill() {
return skill;
}
public void setSkill(String skill) {
this.skill = skill;
}
public Object getCnic() {
return cnic;
}
public void setCnic(Object cnic) {
this.cnic = cnic;
}
public String getContactNumber() {
return contactNumber;
}
public void setContactNumber(String contactNumber) {
this.contactNumber = contactNumber;
}
public String getLabourAmount() {
return labourAmount;
}
public void setLabourAmount(String labourAmount) {
this.labourAmount = labourAmount;
}
public Integer getPerdayProject() {
return perdayProject;
}
public void setPerdayProject(Integer perdayProject) {
this.perdayProject = perdayProject;
}
public String getCompanyAmount() {
return companyAmount;
}
public void setCompanyAmount(String companyAmount) {
this.companyAmount = companyAmount;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getCategoryName() {
return categoryName;
}
public void setCategoryName(String categoryName) {
this.categoryName = categoryName;
}
public String getCityName() {
return cityName;
}
public void setCityName(String cityName) {
this.cityName = cityName;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCountryName() {
return countryName;
}
public void setCountryName(String countryName) {
this.countryName = countryName;
}
public String getDistrictName() {
return districtName;
}
public void setDistrictName(String districtName) {
this.districtName = districtName;
}
}
推荐阅读
- azure - Kusto 查询:根据时间检索最近的 2 次运行并进行汇总
- javascript - 根据条件更改 Bootstrap 弹出框
- ios - iPad 上的 SwiftUI 弹出框随设备旋转而移动
- python - Python重复字典
- javascript - 两个数组包含在单个查询谷歌云火库中。应用在搜索中排序的过滤产品
- mysql - 将 SSMS .rpt 文件加载到 MySQL
- reactjs - 在我的 React.js 应用程序中的身份验证页面之外的每个页面上呈现导航栏
- python-3.x - 如何通过 selenium 和 Python3 在 Chrome 下载 (chrome://downloads) 中获取文件的直接下载链接?
- mysql - 如何制作一个包含 mysql、wordpress 和 phpmyadmin 图像的容器?
- reactjs - 如何向 react-router-dom 添加属性?