Job tried hard to project a certain image. This was not entirely wrong, but despite his righteousness, his projection was far from the perfection that he may have thought he was showing, as the testimony of his three "friends" indicates. In fact, it was fraught with a major failure in his heart, which God clearly saw and determined to cleanse him of.
Suddenly, Job's image of himself is shattered before his eyes. What is he to do? Should he defend his image of himself or repent? To repent may have been quite embarrassing, but as the story unfolds, we see that Job does not perceive that anything is wrong with him. Even if something were wrong, it would have been a major embarrassment to have it exposed. He is so aware of himself as a human being that, for quite a while, he does not perceive that the problem resides in his heart. Therefore, he does what we all do: He defends and justifies himself.
The book of Job is the story of the destruction of Job's self-image. It can also be summed up as the book of human nature. His friends are unsuccessful in their efforts because they perceive his condition as being the result of the sins that he has committed; what he is going through is just retribution for conduct that Job has managed to hide from others for many years. However, God's comments to Satanin Job 1:8 and 2:3 reveal that this is not so: Job is an unusually righteous person, as far as the conduct of his life is concerned.
The problem is not what Job was doing but a flaw in what he was. His defect is not one of outward action but of inward thought, especially in how he perceived himself in relation to God, but also to fellow man. It is a matter of the heart.