I am getting a NullPointerException while creating a TreeMap.

Here is my code: 

```
public TreeMap<AccountGroupBean,List<AccountBean>> getAccountsAndGroups() throws SessionExpiredException {
    TreeMap<AccountGroupBean,List<AccountBean>> map = new TreeMap<AccountGroupBean,List<AccountBean>>();
    List<AccountGroupBean> groups = getAccountGroups();
    for(AccountGroupBean group : groups) {
        List<AccountBean> accounts = getAccountsByGroupId(group.getId());
        System.out.println("PRINT"+ accounts.size());           
        map.put(group,accounts);
        System.out.println("!" +map.get(group).size());
    }
    return map;
}

```

The first println prints 44. That it is to say is not null. However, the second println raises the null exception. 

Any idea what I am doing wrong?

Solution
========

AS pointed in the accepted solution. The problem was in my implementation of compareTo. 

I used to have:

```
public int compareTo(AccountGroupBean o) {
    return (number > o.getNumber()) ? 1 : -1;       
}

```

Adding a 0 return solved the issue:

```
public int compareTo(AccountGroupBean o) {
    if(number == o.getNumber()) {
         return 0;
    }
    return (number > o.getNumber()) ? 1 : -1;       
}

```