2016-02-29 33 views
-2

我試圖將錯誤消息彈出說異常的空白文本框

Error: Amount is required

當他們在盒子還沒有添加量破例。以下是我有:

String output = "Gas Amount: $" + gasAmount + 
         "\nCar Wash: " + price; 
    try { 
     throw new AmountException(); 
    JOptionPane.showMessageDialog(null, output); 
    } 
    catch (AmountException ae) { 
    JOptionPane.showMessageDialog(null, ae.toString(), "Error", JOptionPane.ERROR_MESSAGE);  
    } 
     } 

回答

0

事情是這樣的:

public class AmountException extends Throwable 
{ 
    public AmountException() 
    { 
    super("Error: Amount is required!!"); 
    } 
} 

在您的代碼:

try 
{ 
    if (gasAmount == null) 
    throw new AmountException(); 
    JOptionPane.showMessageDialog(null, output); 
} 
catch (AmountException ae) 
{ 
    JOptionPane.showMessageDialog(null, ae.getMessage(), "Error", JOptionPane.ERROR_MESSAGE); 
} 
+0

我也補充一點,我的代碼,但現在我的錯誤信息不會彈出。 –

+0

你確定你的'gasAmount'等於null嗎?它是一個Number類或原始類型的實例嗎?也許它等於0 ... –