0

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!

1 Answers1

1

Try to check below line

 String name = industries.getName();

Here industries object might be null that's why you are getting this error. and if you have to show just one item then you don't need to use adapter , But if there are more then you can use it like below :-

private Context context;
private ArrayList<Industry> industryList;

public IndustryAdapter(Context context, Industry industry) {
    industryList = new ArrayList();
    industryList.add(industry);
    this.context = context;
}
 public IndustryAdapter(Context context, ArrayList<Industry> industryList) {
    this.industryList=this.industryList;
    this.context = context;
}

@Override
public int getCount() {
    return industryList.size();
}

@Override
public Object getItem(int position) {
    return position;
}

@Override
public long getItemId(int position) {
    return position;
}

@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 = industryList.get(position).getName();
    String descr = industryList.get(position).getDescription();


    tvName.setText(name);
    tvDescription.setText(descr);

    return convertView;
}