0

I have a 2d array

ArrayList<List<Customer>> list;

Each customerNode stores two customers. I want to add a new list of customers to the list array if neither of the customers exist in any of the current List<Customer> in list. However, when I run this it doesnt add the 'newR' to the list.

for(customerNode s: customers){
    count++;
    if(!LinearSearch2(s.getOne(), s.getTwo())){
        System.out.println("Test");
        ArrayList<Customer> newR = new ArrayList<Customer>();
        newR.add(s.getOne());
        newR.add(s.getTwo());
        list.add(newR);
    }
}

thanks in advance!

0

2 Answers 2

2

This is your problem:

List<Customer> newR = null;
newR.add(s.getOne());

You need to initialize newR by assigning it a new instance of List, such as new ArrayList<Customer>();.

Otherwise, your newR reference is pointing to null and you can't invoke methods on it without the JVM throwing a NullPointerException.

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

1 Comment

I just realised that as soon as I posted, the problem is that it still doesn't add the 'newR' to my list, although I am not given any errors
0

Fixed: the problem was with a loop elsewhere in my code

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.