0

I'm trying to do a search bar that filter a listview But I'm facing this error in this line filteredProjects.add(container.getProjects().get(i)); and I can't figure out why? Any idea what the problem might be? Thank you for helping I would apreciate it.

    Gson gson = new GsonBuilder().setDateFormat(
            "yyyy-MM-dd'T'HH:mm:ssZ").create();

    final ProjectContainer container = gson.fromJson(resultat,
            ProjectContainer.class);
    final ListView lv = (ListView) findViewById(R.id.list);

    /**
     * Updating parsed JSON data into ListView
     * */
    adaptateur = new ProjectAdapter(ProjectActivity.this,
            R.layout.ligne_project, container);

    lv.setAdapter(adaptateur);

    // setListAdapter(adaptateur);
    // Search EditText
    EditText inputSearch;
    inputSearch = (EditText) findViewById(R.id.inputSearch);
    /**
     * Enabling Search Filter
     * */
    inputSearch.addTextChangedListener(new TextWatcher() {

        @SuppressWarnings("null")
        @Override
        public void onTextChanged(CharSequence s, int arg1, int arg2,
                int arg3) {
            ProjectContainer filteredProjects = null;

            if (!s.toString().equals("")) {
                for (int i = 0; i < container.getProjects().size(); i++) {
                    if (container.getProjects().get(i)
                            .getAbbreviation().toString().contains(s)) {
                        filteredProjects.setProjects(null);
                        filteredProjects.add(container.getProjects()
                                .get(i));
                    }
                }
                adaptateur = new ProjectAdapter(ProjectActivity.this,
                        R.layout.ligne_project, filteredProjects);

                lv.setAdapter(adaptateur);
            } else {
                adaptateur = new ProjectAdapter(ProjectActivity.this,
                        R.layout.ligne_project, container);
                lv.setAdapter(adaptateur);
            }

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1,
                int arg2, int arg3) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

And this is my ProjectContainer.class :

import java.util.List;

import com.google.gson.annotations.SerializedName;

    public class ProjectContainer {
        @SerializedName("project")
        List<Project> projects;

        public List<Project> getProjects() {
            return projects;
        }

        public void setProjects(List<Project> projects) {
            this.projects = projects;
        }

        public void add(Project project) {
            projects.add(project);
        }
    }

3 Answers 3

1

You need to instantiate filteredProjects. Try changing

ProjectContainer filteredProjects = null;

to

ProjectContainer filteredProjects = new ProjectContainer();

You are also setting the projects list in filteredProjects to null at this line:

filteredProjects.setProjects(null);

This will cause a NullPointerException when you call filteredProjects.add(..). You need to assign a new listm instead of null.

filteredProjects.setProjects(new List<Project>());

I can recommend this answer about null values if you aren't entirely sure what it is.

Sign up to request clarification or add additional context in comments.

Comments

1
  public void add(Project project) {
        projects.add(project);
    }

In the above line you used an uninitialized instance projects.

So better call setProjects method before calling add method

1 Comment

Thanks for your reply, but I updated my question and I called setProjects method before calling add method but I got java.lang.NullPointerException in this line filteredProjects.setProjects(null);
1

You aren't calling setProjects in the code snippet above, just defining it. Therefore, uninitialised objects are null, per the language definition, and will throw a NullPointerException when access is attempted.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.