java.lang.NullPointerException:尝试调用接口方法';int java.util.List.size()';在从服务器到微调器的空对象引用上
我试图从服务器获取数据到可搜索微调器,但显示为空。我正在使用 改装并检查postman上的api,它显示数据,但在android studio中显示空。我只显示mysql数据库中的国家名称,抱歉英语不好 谢谢你提前解决我的问题java.lang.NullPointerException:尝试调用接口方法';int java.util.List.size()';在从服务器到微调器的空对象引用上,java,mysql,xml,retrofit2,android-studio-3.0,Java,Mysql,Xml,Retrofit2,Android Studio 3.0,我试图从服务器获取数据到可搜索微调器,但显示为空。我正在使用 改装并检查postman上的api,它显示数据,但在android studio中显示空。我只显示mysql数据库中的国家名称,抱歉英语不好 谢谢你提前解决我的问题 public class LabourRegActivity extends AppCompatActivity { private ArrayList<String> countryname = new ArrayList<String>
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) {
}
});
}
}
公共类LabourReActivity扩展了AppCompatActivity{
私有ArrayList countryname=新ArrayList();
可搜索的微调器国家、类别、城市、项目基地、地区;
编辑文本名称、联系人、cnic、每日费率、地址;
阵列适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u labour\u reg);
countryS=findViewById(R.id.country\u微调器);
类别=findViewById(R.id.skill\U spinner);
cityS=findviewbyd(R.id.city\u微调器);
projectBase=findViewById(R.id.projectBase);
district=findViewById(R.id.district\U微调器);
name=findviewbyd(R.id.Edt_name);
联系人=findViewById(R.id.Edt\U联系人);
cnic=findViewById(R.id.Edt_cnic);
每日费率=findViewById(R.id.Edt_/天);
地址=findViewById(R.id.Edt\U地址);
CountryFetchApi fetchApi=ApiClient.getClient().create(CountryFetchApi.class);
//HashMap=newHashMap();
Log.d(“ok”、“countryMap”+fetchApi);
调用模型=fetchApi.getcountryrecord();
model.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
列表数据=response.body();
Log.d(“ok”,“country”+response.body());
对于(int i=0;i
您能为您收到的CountryModel
和json响应添加代码吗?可能是映射不正确
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;
}
}