The contacts on the camera body and lens are spring loaded to allow for lens attachment and removal. It is possible that one or more of the contacts is depressed or is not making contact to the opposite contact. It is also possible that the lens in not fully and correctly seated and contact is intermittent.
The voltages use for IC circuits is very low and invisible dirt or oxide may prevent proper contact. A visual inspection might not see the dirty contact. Many use a liquid contact cleaner and oxide inhibitor.
It could also be a difficult focus target to detect. Not enough contrast or there could be an object infront of the subject that makes focusing dificult, like bars, branch, or glass.