title: Populating a TreeMap throws NullPointerException
body: <p>I am getting a NullPointerException while creating a TreeMap.</p>
  
  <p>Here is my code: </p>
  
  <pre><code>public TreeMap&lt;AccountGroupBean,List&lt;AccountBean&gt;&gt; getAccountsAndGroups() throws SessionExpiredException {
      TreeMap&lt;AccountGroupBean,List&lt;AccountBean&gt;&gt; map = new TreeMap&lt;AccountGroupBean,List&lt;AccountBean&gt;&gt;();
      List&lt;AccountGroupBean&gt; groups = getAccountGroups();
      for(AccountGroupBean group : groups) {
          List&lt;AccountBean&gt; accounts = getAccountsByGroupId(group.getId());
          System.out.println("PRINT"+ accounts.size());           
          map.put(group,accounts);
          System.out.println("!" +map.get(group).size());
      }
      return map;
  }
  </code></pre>
  
  <p>The first println prints 44. That it is to say is not null. However, the second println raises the null exception. </p>
  
  <p>Any idea what I am doing wrong?</p>
  
  <h1>Solution</h1>
  
  <p>AS pointed in the accepted solution. The problem was in my implementation of compareTo. </p>
  
  <p>I used to have:</p>
  
  <pre><code>public int compareTo(AccountGroupBean o) {
      return (number &gt; o.getNumber()) ? 1 : -1;       
  }
  </code></pre>
  
  <p>Adding a 0 return solved the issue:</p>
  
  <pre><code>public int compareTo(AccountGroupBean o) {
      if(number == o.getNumber()) {
           return 0;
      }
      return (number &gt; o.getNumber()) ? 1 : -1;       
  }
  </code></pre>