I would like to dynamically fetch the parent Account of an Account in a similar fashion to the way it's done in the selected answer to this question. However, this doesn't seem to work in this case. The line right before the debug will throw a NullPointerException, no matter I use ParentId, Parent, Account, or ParentAccount.
Account topAccount = new Account(Name = 'top');
insert topAccount;
Account childAcc = new Account(Name ='Acme', Parent= topAccount);
insert childAcc;
Contact c1 =new Contact(LastName = 'T', AccountId = childAcc.Id);
insert c1;
SObject c2 = [select Account.Parent.Name from Contact where ID =:c1.ID];
String topAccName = (String)c2.getSObject('Account').getSObject('ParentId').get('Name');
System.debug('****Top acc name: ' + topAccName);