2 Chronicles 35:20
After all this, when Josiah had prepared the temple, Necho king of Egypt came up to fight against Carchemish by Euphrates: and Josiah went out against him. After all this, when Josiah had prepared the temple, Necho king of Egypt came up to fight against Carchemish by Euphrates: and Josiah went out against him.
After he had prepared the temple, Josiah went out to oppose Necho king of Egypt when Necho came up to fight against Carchemish by the Euphrates. Necho declared that he was not fighting against Judah and warned Josiah not to meddle or interfere with his campaign. That warning was in fact a message from God, but Josiah did not heed it and instead in self-confidence chose his own course.
The battle at Megiddo was one he should never have entered, and he lost his life as the direct consequence of disregarding God's word. His prior faithfulness in reform and preparation did not excuse this act of self-reliance. God's promise of good things to him did not protect him when he acted contrary to the divine warning.