Why do I get a sharp pain on my upper rib cage? This happens randomly.

Not. Sure such pain can be due to costochondritis or muscular skeletal injuries. failure to resolve with OTC analgesics is an indication for evaluation by your PCP.
Muscle spasm . Usually, short sharp pains in the ribs is from muscle spasm in one of the many different muscles that work between or over the rib cage. They are not serious problems, but they are a pain. Good health .