The most accurate method for diagnosing an ulcer is endoscopy. This technique allows for direct visualization of the lesion and the collection of tissue samples when appropriate. In some cases, a barium meal X-ray of the stomach and duodenum may be used as an adjunct to endoscopy. Early diagnosis of ulcers through endoscopy is crucial for preventing their progression into stomach cancer.