To also check for blanks try
=IF(OR(Agency_Contract!X9=0,Agency_Contract!X9="")*1=1,"XXX",IF(J9>Agency_C ontract!X9,J9-Agency_Contract!X9,VLOOKUP(D9,Agency_Contract,22,1)))
Explanation
The bit inside the OR statement returns a TRUE value if both conditions are met, and FALSE otherwise
To turn that into something we can use we multiply by 1 and TRUE becomes the number 1 and FALSE becomes the number 0