I understand that you don't want to use DuplicateImageDocument because you might get errors when you have a null target, but I still feel like this is your best option. If you want to make sure you don't throw any errors, do some empty checks to make sure that neither the source nor the target can be null, and that should solve your issue.
Also be sure to add proper error handling, though that's always recommended for java actions either way.
You can use community common you can download it from marketplace