Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i'm currently trying to add items to an generic "array" of arraylists but for some reason i keep getting a null pointer exception. The Structure is initialised and both my array index reference and the reference to the object i'm passing in are both visible within the body of code right before the exception occurs. I'm almost sure its down to the way i either declared the data structure or my way im trying to add it in. Any advice would be appreciated. Thanks in advance

ArrayList<Site>[] group = (ArrayList<Site>[])new ArrayList[entranceSites.size()];

group[i].add(sIndex(path));

sIndex is a function I'm using to convert integers to graph sites and the object is not null when I'm passing it in so i'm sure its not the problem. I is initialised and also visible to the program.

share|improve this question
    
Why does it need to be an array of ArrayList? Why not ArrayList<ArrayList<Site>>?? –  MadProgrammer Oct 24 '14 at 0:42
    
I would say that group[i] is null as you've not created an ArrayList for that element...but your code is so incomplete as to not to be able to make a judgment call... –  MadProgrammer Oct 24 '14 at 0:43
    
To be honest i'm new to the ArrayList datastructure and felt more comfortable using a generic array as a container. Is it an undesirable approach? –  user2209521 Oct 24 '14 at 0:44
    
It's just more work and since you're already using an ArrayList... –  MadProgrammer Oct 24 '14 at 0:44
    
Yes, it is undesirable. Generics and arrays do not play nicely together. I would use a List<List<Site>>. –  David Conrad Oct 24 '14 at 1:57

4 Answers 4

up vote 2 down vote accepted

You have allocated an array of ArrayLists, but you have not allocated any actual ArrayLists inside that array. The array initially contains all null references. So your invocation to add is on a null reference and thus causes the exception. If you say:

group[i] = new ArrayList<Site>();

Before you call add it will work.

Note that it is generally a bad idea to mix primitive arrays and Java collections, and if you are new to Java, then you should probably stick to collections since they are going to be easier to work with.

You should also be aware that the cast you are making (ArrayList<Site>[]) is unchecked and will almost certainly generate a warning assuming you have warnings enabled, which you should be enabling warnings as a beginner. This is another reason why it is not a good idea to mix generics with primitive arrays.

share|improve this answer
    
Your solution proved to be the best. <3 –  user2209521 Oct 24 '14 at 1:14
new ArrayList[entranceSites.size()];

does not actually initialize the array elements with any constructor. The array will be filled with enteranceSites.size() null elements.

You will need to iterate through the array and actually construct ArrayList objects.

share|improve this answer
    
Thank you, that makes sense. –  user2209521 Oct 24 '14 at 0:45

Here's how you can set each element of the array to a new ArrayList using Java 8:

Arrays.setAll(group, n -> new ArrayList<Site>());

(The second argument is a function of n, the array index, but n isn't actually used. You still need to include it.)

share|improve this answer
    
Thanks a lot bud, you saved the day! Ive been researching how to add to an arraylist of arraylists and ive hit so many dead ends. I fear to ask any more questions here in case i get my ability to question discontinued :( –  user2209521 Oct 24 '14 at 1:05

By the looks of your code fragment, my guess is that you failed to initialize the ArrayList<Site> element being added to the array; thus, failing when calling the List.add() method. The array itself is properly initialized, but you are trying to add a Site to an ArrayList that has not been initialized properly.

For this to work, you must create your ArrayList<Site> object. Once your lists are properly instantiated, you can add them to the array. You can add Site objects when creating the list or after you add them to the array. It does not matter when because the space in memory will be already allocated. Suppose a company has sites in many states, for argument sake, New York. All of the sites in that geographical location will be added to the NY list of sites:

ArrayList<Site> nySites = new ArrayList<Site>();
Site site1 = new Site();
group[0] = nySites;
group[0].add(site1);  // Now you can call the add() method

I hope this helps.

share|improve this answer
    
Your help is much appreciated. –  user2209521 Oct 24 '14 at 14:57
    
You're welcome. The one thing to remember is to always instantiate objects properly before using. In your case, ArrayList<Site>[], there are three objects that need to be instantiated: the array, the ArrayList, and the Site. It is a common mistake to instantiate the array and not the objects it may contain. –  hfontanez Oct 24 '14 at 17:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.