Give the barrel a tug. Does the barrel wobble? If it moves, the nut isn't screwing on far enough. If it doesn't, the nut is out of spec.
If it's the upper, I would guess that the threads on the upper we're not completed or finished. It could be as simple as too much material/finish on the threads. Have you tried a different barrel nut yet? If a different nut fits, then the original nut needs to be addressed. If the second nut doesn't work out, the upper is out of spec.
A barrel nut should be worked on and off 3 or 4 times to ensure its seating all the way back.
You can clean up those threads with a wire brush really fast. A brass wheel on a dremmel is even faster.
Needless to say. The gap is definitely too large.