0

I am writing a JSF 2.2 project on Eclipse Neon. I have two arraylists; one is fetched from group of checkboxes that are checked by the user(arrayList1) and the other one is fetched from keywords that are entered into chips component(arrayList2) by user inside .xhtml page. What I need to do is I have to combine those two arraylists.

The problem is when I leave chips empty, arraylist2 returns null while arraylist1 returns empty string when I leave the boxes unchecked as well. So I think the problem is related to else if (lengthlist1 > 0 && arrayList2.size() == 0) part because all the other cases functioning well. If check the checkboxes and leave the chips component empty and then run the code, I get the following error:

Caused by: java.lang.NullPointerException

Again, all the other if - else statements are working fine.

I basically have 4 cases:

    private String wordx;
    private List<String> arrayList1;
    private List<String> arrayList2;

    public void arrayList2List() {

            arrayList2 = new ArrayList<String>();
        }

        public void concatenate() throws IOException {

                int lengthlist1 = arrayList1.size();
                int lengthlist2 = arrayList2.size();

                ArrayList<String> concat = null;

                if (lengthlist2 > 0 && lengthlist1 > 0) {
                    concat = new ArrayList<>(lengthlist1 + lengthlist2);

                    for (int i = 0; i < lengthlist1; i++) {
                        concat.add(arrayList1.get(i) + And + wordx);

                        for (int l = 0; l < lengthlist2; l++) {
                            concat.add(arrayList2.get(l) + And + wordx);
                        }
                    }   
                } else if (lengthlist1 > 0 && arrayList2.size() == 0) { /* I think problem occurs here, when I leave checkboxes list empty */
                    concat = new ArrayList<>(lengthlist1);
                    for (int k = 0; k < lengthlist1; k++) {
                        concat.add(arrayList1.get(k) + And + wordx);
                    }

                } else if (lengthlist1 <= 0 && lengthlist2 > 0) {
                    concat = new ArrayList<>(lengthlist2);
                    for (int m = 0; m < lengthlist2; m++) {
                        concat.add(arrayList2.get(m) + And + wordx);
                    }

                } else {
                    TODO:do something else
                }
                for (int j = 0; j < concat.size(); j++) {
                    Search(concat.get(j));
                }

            }

Can someone explain the root cause of the error? Where do I go wrong? All help will be appreciated.

KontrCode
  • 89
  • 11
  • You didn't initialized arrayList1 ... please correct it – Amit May 17 '18 at 09:06
  • I already have it. I just didn't write it there because it is not the reason for the error. – KontrCode May 17 '18 at 10:17
  • I have solved it; I have changed `int lengthlist2 = arrayList2.size();` with `int lengthlist2 = 0;` and added the following code piece: `if(arrayList2 == null){ lengthlist2 = 0 ; } else{ lengthlist2 = Arraylist2.size(); }` – KontrCode May 17 '18 at 12:05

0 Answers0