Bad things do happen to righteous people but we must remember these truths:
James 1:2-4, Consider it pure joy, my brothers, whenever you face trials of many kinds, because you know that the testing of your faith develops perseverance. Perseverance must finish its work so that you may be mature and complete, not lacking anything.
Romans 5:3-5, And not only this, but we also exult in our tribulations, knowing that tribulation brings about perseverance; and perseverance, proven character; and proven character, hope; and hope does not disappoint, because the love of God has been poured out within our hearts through the Holy Spirit who was given to us.
Matthew 5:12, Rejoice and be glad, because great is your reward in heaven, for in the same way they persecuted the prophets who were before you.
Embracing Trials sermon by Bryan Clark from the Lincoln Berean Church: