Conferring with one of my colleagues, this looks to be encountered from bad data in the database; such as categories exist for a database but not the database itself. If you remove that bad data, the upgrade will work without issue. Unfortunately, that is the problem with most version 3 upgrades as there is a lot happening in most data due to outside influences or configurations than what we can account for in the version 4 upgrader. As what you've encountered is really a custom migration than an upgrade, we're happy to bring this to a ticket to discuss a quote. However, standard support is no longer offered for version 3, which includes upgrades from that version.
Sorry that this is not what you wish to hear but hope this helps guide you to a solution.