3

Cant get around this. Null Pointer exception occurs when trying to add a String object to an ArrayList. Trouble is, I believe I have already initialized the 'temp' ArrayList properly. Please help.

void initialConbinations(String[] headers) {
    //Dataset is SN,FN,RN,PN
    //Map the headers to the appropriate portions
    String[] newHeaders = {"SN,high", "SN,medium", "SN,low", "FN,high", "FN,medium", "FN,low", "RN,high", "RN,medium", "RN,low"};
    List<List> assRuleHeaders = new ArrayList<List>();
    ArrayList<String> temp = new ArrayList<String>();

    //Use bitwise shifting to do the rest
    int k = 0, l = 0;
    for (int i = 1; i < 511; i++) {
        for (int j = 0; j < 9; j++) {
            l = i;
            k = (l >> j) & 0x00000001;
            if (k == 1) {
                System.out.println(k + " , " + i + " , " + j);
                System.out.println(newHeaders[j]);
                temp.add(newHeaders[j]);    //Getting a Null Pointer Exception here
            }
        }
        assRuleHeaders.add(temp);
        temp = null;
    }

    for (int i = 0; i < assRuleHeaders.size(); i++) {
        this.printArray(assRuleHeaders.get(i));
    }
}
2
  • There's a lot of superfluous code in there. Mind cleaning it up a bit to make it easier to see? Also, mind pasting the print stream? Commented Apr 26, 2013 at 15:51
  • Sorry about that. Newbie here, will be more careful next time. Commented Apr 26, 2013 at 15:57

2 Answers 2

12

The error is with the line

temp = null;

You are not reinitializing temp again.

if you move the line

ArrayList<String> temp = new ArrayList<String>();

between the two for loops all should be fine.

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

Comments

3

At the last step of i loop you are setting temp to null instead of setting it to a new array using new ArrayList<String>();. This will cause temp.add(newHeaders[j]); to through the null pointer exception.

change

temp = null;

to

temp = new ArrayList<String>();

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.