My fix for the dreaded 5002 error:
NOTE: If you screw up and wipe out your Apps, you're SOL. You should also know that iTunes may want to reinstall all your applications on your iPhone/iPod, so it might be best to hold off doing this until you're ready for this (and bear in mind that my experience with iPhone application remove/install hasn't been stellar, and I've had to completely restore from scratch a few times).
1. Back up Mobile Applications from /Users/myusername/Music/iTunes by dragging them to another disk -- simply moving them elsewhere on the same disk doesn't work, since iTunes keeps track of their location. Duplicating the folder would likely work too, if you haven't got anywhere else to copy them.
2. Once you have the Mobile Applications folder backed up, and have verified and reverified that you've got a copy and haven't simply moved the folder, open iTunes WITHOUT YOUR IPHONE/IPOD CONNECTED and select all your applications and delete them. (if your iPhone/iPod were connected, it would now start helpfully deleting apps)
3. When they're all gone, quit iTunes
4. Start iTunes again and verify that you have no apps listed.
5. Drag your backup Mobile Applications folder to your open iTunes window -- if you have default iTunes settings (copy/manage my music), then iTunes will start re-adding your applications. The nice thing about this is that it also eliminates all the duplicate apps and renames along the way -- all those updates you keep doing don't get rid of older versions, so these just pile up as "Application 1", 2, 3, etc. You may end up saving a lot of disk space. Many apps have gone through name changes too, so you may clear up some confusion and duplication too.
6. Read the iTunes notices carefully -- you will likely get something like "an OLDER version of this app already exists, do you want to replace it" (you DO), or "a NEWER version of this app already exists, do you want to replace it" (you DON'T), or sometimes a mention that this app already exists as the same version (replace it).
7. Quit iTunes, then restart it to "set" the changes
You will end up with a pristine iTunes Mobile Applications folder at the end of this process, and will likely save some space too.
Your 5002 error should also be gone too.
REMEMBER THAT YOU COULD POTENTIALLY LOSE ALL YOUR APPLICATIONS IF YOU SCREW UP, so please do this at your own risk, and as always, make sure you have backups!