Here is a paraphrased segment from Joseph Caryl's Practical Observations on Job, Vol 2:
Job has three friends that counsel him in his distress. One of the confusing aspects of this book of the Bible is that his friends' counsel seems doctrinally sound, but we know from the book's conclusion that they are in error. How can this be? The answer is that there are actually four principles the friends and Job discuss, and the friends actually agree with Job on three of the four principles. The fourth, however, and the most crucial, is where the error lies. Here are the first three principles where Job concurs with his three friends:
1. All afflictions and calamities that befall man fall within the eye and certain knowledge of God.
2. God is the author and efficient cause, the orderer and disposer of all afflictions and calamities.
3. In regard of his most holy majesty and unquestionable soveriegnty, God neither does nor can do any wrong or injury to any of his creatures, whatsoever affliction he lays or however long he is pleased to continue it upon them.
The third one may be shocking, but let's look at them again in an even briefer context:
1. God knows all man's afflictions.
2. God causes all man's afflictions.
3. These afflictions are not wrong.
In other words, the afflictions which God ordains are for our good. Now, here is the fourth principle, which the friends hold with the other three, but which Job utterly denies. This fourth principle has two parts:
1. Whoever does good receives good reward to the measure of the good he has done, and whoever does evil is rewarded with evil equal to the evil he has done. This is karma.
2. Whenever a wicked man seems to prosper, it is only momentary, and he will soon (in this life) be afflicted. Also, whenever a godly man faces adversity, it is only momentary, and he will suddenly (in this life) be blessed. Observation and experience reveal this not to be so.
This builds up the fourth principle: because Job is greatly and lengthy afflicted, therefore he is numbered with the wicked.
Job disagrees with this last principle, and Job's view is doctrinally sound. It is this:
The providence of God dispenses outward prosperity and affliction so indifferently to good and bad, to the righteous and to the wicked, the no unerring judgment can possibly be made up of any man's spiritual estate by the face and upon the view of the temporal.
Afflictions happen to the godly and the wicked alike. For the wicked, the afflictions are judgments. For the godly, they are disciplines that perfect the believer more into the likeness of Christ.