I working with an app but I have a problem. When I press the back button my app crashes and gives me Null Pointer Exception error. I have a list view with some data from api where I used retrofit2 to call them. Everything works fine except the back button. Here is what I did: My adapter
private Context context;
private Industry industries;
public IndustryAdapter(Context context, Industry industries) {
this.industries = industries;
this.context = context;
}
@Override
public int getCount() {
return 1;
}
@Override
public Object getItem(int position) {
return 1;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
convertView = inflater.inflate(R.layout.row_item, parent, false);
}
TextView tvName = (TextView)convertView.findViewById(R.id.tvName);
TextView tvDescription = (TextView)convertView.findViewById(R.id.tvDescription);
String name = industries.getName();
//industries.setName(name);
String descr = industries.getDescription();
//industries.setDescription(descr);
tvName.setText(name);
tvDescription.setText(descr);
return convertView;
}
and in my activity I have this
CompaniesRetriver companiesRetriver = new CompaniesRetriver();
this.recyclerView = (RecyclerView) findViewById(R.id.companies_list);
SharedPreferences editor = getSharedPreferences(MY_PREFERENCE, MODE_PRIVATE);
String token = editor.getString("token", "");
final Context sfdsf = this;
Callback<Companies> callback = new Callback<Companies>() {
@Override
public void onResponse(Call<Companies> call, Response<Companies> response) {
progressBar.dismiss();
if(response.isSuccessful()) {
progressBar.dismiss();
Companies companies = response.body();
CompanyAdapter adapter = new CompanyAdapter(CompaniesListActivity.this, companies);
recyclerView.setLayoutManager(new LinearLayoutManager(CompaniesListActivity.this));
recyclerView.setAdapter(adapter);
System.out.println(companies);
} else {
System.out.println(response.body());
}
}
@Override
public void onFailure(Call<Companies> call, Throwable t) {
progressBar.dismiss();
System.out.println(t.getLocalizedMessage());
}
};
companiesRetriver.getCompanies(callback, token);
My Log:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String al.sqmo.model.Industry.getName()' on a null object reference
my POJO class
@SerializedName("name")
private String name;
@SerializedName("id")
private int id;
@SerializedName("description")
private String description;
public Industry(String description, String name, int id) {
this.description = description;
this.name = name;
this.id = id;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
Any help is appreciated!