title: ArrayList of ArrayList in Java
body: <p>I'm new to Java, coming from a C/C++ background.  I'm trying to write a music player app for Android and I am working on a library scanning function.  I want to have a hierarchical database of the format Artist -> Album -> Song where Artist has a name and a group of Albums, an Album has a name, year, and group of Songs, and a Song has a title, track number, and file location.</p>
  
  <p>I created three classes to store this information:</p>
  
  <pre><code>public class libraryElementArtist
  {
      public String name;
      public ArrayList&lt;libraryElementAlbum&gt; albums;
  }
  
  public class libraryElementAlbum
  {
      public String name;
      public String year;
      public ArrayList&lt;libraryElementSong&gt; songs;
  }
  
  public class libraryElementSong
  {
      public String name;
      public int num;
      public String filename;
  }
  </code></pre>
  
  <p>The idea to fill them is simple - scan through each file and add its artist first, then album, then song.  Each time it checks to make sure the artist/album does not already exist before creating a new one.</p>
  
  <p>Essentially, I start off creating an ArrayList to store the artist information like this:</p>
  
  <pre><code> ArrayList&lt;libraryElementArtist&gt; libraryData = new ArrayList&lt;libraryElementArtist&gt;();
  </code></pre>
  
  <p>Then, to add an artist to the database:</p>
  
  <pre><code>libraryElementArtist newEntry = new libraryElementArtist();
  newEntry.name = song_artist;
  libraryData.add(newEntry);
  </code></pre>
  
  <p>And then to add an album:</p>
  
  <pre><code>libraryElementAlbum newEntry = new libraryElementAlbum();
  newEntry.name = song_album;
  libraryData.get(artistIndex).albums.add(newEntry);
  </code></pre>
  
  <p>where artistIndex is the index of the album's artist in the top-level artist array.</p>
  
  <p>When I run this on the device and step through it in the debugger, the libraryElementArtist items are inserted into the libraryData array and their names are correctly filled in.  However, the albums field is listed as null and trying to add albums does not fill in any data.</p>
  
  <p>Sorry if this is a noob question, like I said I'm new to Java and I've searched and can't find what I'm looking for.  Also, this is the way I'd do such a task in C++, not sure if it's the correct Java way.</p>