The Bible tells us of a man of God named David, who was a shepherd of sheep, then became the armourer of Saul, and of course, the great king of Israel. The Bible also teaches us that this man was according to the heart of God. He was an extremely courageous man and full of faith in God. But none of these things keep us from erring, because to err is human.
During his reign he made serious mistakes and of course, he was admonished by God for them. Remember that our God does not coexist with sin, since He is holy.
Even though David made great mistakes, this was never an excuse for him to forget God completely, but David had something very special, and that was that at certain moments he knew to go before the presence of God to heal his error and try not going back to committing it.