Trying to build what I thought would be a simple trigger: when a task is created that is only related to a contact (WhoId = contact type, but WhatId = null), relate the task's WhatId to the contact's account. I know I can't just set the WhatId equal to AccountId without querying for it (and that in an after trigger), so I wrote the following query, which is saving without error.

However, once I save the trigger and create a test Task in my sandbox, I get a null pointer exception. It's throwing here on line 22, column 1 -- where I set the WhatId = c.AccountId. 

Can someone point out what I'm missing here?

```
trigger addAccountToTask on Task (after insert) {

Set<Id> taskWhoIds = new Set<Id>();

for (Task t : Trigger.new){
    if(t.WhoId != null){
        if(((String)t.WhoId).startsWith('003')){
                taskWhoIds.add(t.WhoId);}
        }
    }

    List<Contact> aIdList = [SELECT AccountId, Id FROM Contact WHERE Id IN :taskWhoIds];

    Map<Id, Contact> whoToAccountMap = new Map<Id, Contact>();
    for (Contact who : aIdList) {
        whoToAccountMap.put(who.AccountId, who);
    }

    for (Task t : Trigger.new){
        Contact c = whoToAccountMap.get(t.whoId);
        if(t.WhatId == null){
            t.WhatId = c.AccountId;
        }
    }
}

```

Much appreciated!

//
Edited to include fixed trigger. Thank you @crmprogdev for all your help!
//

This trigger is working just as I'd hoped! Hopefully this helps anyone else looking for this. 

```
trigger addAccountToTask on Task (after insert) {

Set<Id> taskWhoIds = new Set<Id>();
for (Task t : Trigger.new){
    if(t.WhoId != null){
        if(((String)t.WhoId).startsWith('003')){
                taskWhoIds.add(t.WhoId);}
            }
        }

    List<Contact> aIdList = [SELECT Id, AccountId FROM Contact WHERE Id IN :taskWhoIds];

    Map<Id, Id> whoToAccountMap = new Map<Id, Id>();
    for (Contact who : aIdList) {
        if(who.AccountId != null){
           whoToAccountMap.put(who.Id, who.AccountId);
        }   
    }
    List<Task> taskList = new List<Task>();
    for (Task t : Trigger.new){
        Task toUpdate = new Task (Id = t.Id);
        Id aId = whoToAccountMap.get(t.whoId);

        if(aId != null){
            if(t.WhatId == null){
                toUpdate.WhatId = aId;
                taskList.add(toUpdate);
                }
            }
        }
        update taskList;
    }

```