You may refer to Bob Cornelissen blog SCOM Object reference not set to an instance of an object, https://blog.topqore.com/scom-object-reference-not-set/.
If you get that error the best you can first do is in SCOM open up a Discovered Inventory view and check how many objects are still there (if it does not throw the same exception as the one I started with!).
Check the number of entries there.
Now run the remove-scomdisabledclassinstance command again.
If it throws the error refresh the screen to check if discovered inventory shows a lower number of discovered instances.
Keep doing that until it finishes.
Next I waited for a grooming interval and let it populate the classes again one by one by turning the discoveries back on.
Roger