Why did God send a three-year famine on David's kingdom?

Because Saul killed the Gibeonites

(There's no record of this killing in the Bible.)
Then there was a famine in the days of David three years, year after year; and David enquired of the LORD. And the LORD answered, It is for Saul, and for his bloody house, because he slew the Gibeonites. 2 Samuel 21:1