Do professors who are in a position to write a recommendation letter for me "have to" write a recommendation letter? For example, my PhD advisor, can he suddenly (without any reason) say that he won't write a letter? I mean does it depend on their will? More specifically, if some professor has promised me a recommendation letter earlier, can he suddenly change his opinion without any reason?
As stated in the other answer, no professor has to write a recommendation letters for a specific student. The most common reason is that it would be a letter the student wouldn't want anyway, ie a bad recommendation.
On the other hand, it is part of the job of a professor to write recommendation letters for students in general. So if a professor states that he/she categorically refuses to writes recommendation letters for anyone that would be a reason for complaint to their dean or head of department.
Writing a reference letter is a courtesy, never an obligation.
It is possible for a thesis director to decline supporting a student for a job. It is usually a bad sign when a director is not writing a reference letter - either because the director has declined or because the student did not ask - and indeed in many cases employers expect a letter from the thesis director.
On the other hand, if the professor promised a letter, he or she is morally bound to provide this letter. Basically, the time to decline is passed and reneging on such a promise is extremely bad form.
There’s nothing to be done if the professor no longer wants to support the student except ask someone else. If things have gotten that far, one can rightly wonder how strong would be the letter of support, and a letter from someone else might be a good way for the student to cut his or her losses.