由于嘗試捕獲多個例外而發生錯誤“區域變數年齡可能尚未初始化”。
你好。上一個問題解決后,我決定對程式做一點小改動,使用 JOptionPane 將控制臺中的列印轉換為簡單的 GUI,結果出現了新的錯誤。我試圖捕獲兩個名為 NumberFormatException 和 NullPointerException 的特定例外。NumberFormatException 是由于在 JOptionPane.showMessageDialog 的空白處輸入了數字以外的字符而發生的,因為該變數已被宣告為 double。NullPointerException 由于單擊 X 符號或取消按鈕而發生。為了捕捉 NullPointerException,我在 NumberFormatException 后面添加了一些額外的代碼。但是,在測驗運行程式時,控制臺中會出現以下錯誤:
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
The local variable age may not have been initialized
at Welcome.main(Welcome.java:10)
實際上,在課程開始時,我已經將變數 age 宣告為 double,盡管如此,“可能尚未初始化”的錯誤仍然存??在。您能否提供此錯誤的解決方案?如果代碼 catch(NullPointerException e){JOptionPane.showMessageDialog(null,"Program cancelled.") 不存在,則不會發生錯誤。
順便說一句,除了主要問題,您能否解釋一下 JOptionPane.showMessageDialog 括號內“null”的含義和用法。非常感謝。我的代碼如下:
import javax.swing.*;
public class Welcome {
public static void main(String[] args) {
double age;
while (true) {
try {
age = Double.parseDouble(JOptionPane.showInputDialog(null, "please enter your age and we will define your age group"));
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input Error. "
"Please enter a number corresponding to your age only.", "error", JOptionPane.ERROR_MESSAGE);
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, "Program canceled.");
continue;
}
if (age < 0) JOptionPane.showMessageDialog(null, "Warning!Negative values cannot hold true.",
"warning", JOptionPane.WARNING_MESSAGE);
else if (age <= 0.1) JOptionPane.showMessageDialog(null, "You are a newborn.");
else if (age <= 1) JOptionPane.showMessageDialog(null, "You are an infant.");
else if (age <= 3) JOptionPane.showMessageDialog(null, "You are a toddler.");
else if (age <= 5) JOptionPane.showMessageDialog(null, "You are a preschooler.");
else if (age <= 13) JOptionPane.showMessageDialog(null, "You are a school-aged child.");
else if (age <= 19) JOptionPane.showMessageDialog(null, "You are an adolescent and a denarian.");
else if (age <= 29) JOptionPane.showMessageDialog(null, "You are a vicenarian.");
else if (age <= 39) JOptionPane.showMessageDialog(null, "You are a tricenarian.");
else if (age <= 49) JOptionPane.showMessageDialog(null, "You are a quadragenarian.");
else if (age <= 59) JOptionPane.showMessageDialog(null, "You are a quinquagenarian.");
else if (age <= 69) JOptionPane.showMessageDialog(null, "You are a sexagenarian.");
else if (age <= 79) JOptionPane.showMessageDialog(null, "You are a septuagenarian.");
else if (age <= 89) JOptionPane.showMessageDialog(null, "You are a octogenarian.");
else if (age <= 99) JOptionPane.showMessageDialog(null, "You are a nonagenarian.");
else if (age <= 109) JOptionPane.showMessageDialog(null, "You are a centenarian.");
else if (age <= 150) JOptionPane.showMessageDialog(null, "You are a supercentenarian.");
else if (age > 150)
JOptionPane.showMessageDialog(null, "Warning!The value entered is too large to be processed.", "warning", JOptionPane.WARNING_MESSAGE);
}
}
}
uj5u.com熱心網友回復:
嘗試將所有年齡驗證登錄移動到try塊內,其中age變數是 100% 定義的。像那樣:
try {
// here you initialise the "age" variable
final double age = Double.parseDouble(JOptionPane.showInputDialog(null, "please enter your age and we will define your age group"));
// here you work with the "age" variable
if (age < 0) JOptionPane.showMessageDialog(null, "Warning!Negative values cannot hold true.",
"warning", JOptionPane.WARNING_MESSAGE);
else if (age <= 0.1) JOptionPane.showMessageDialog(null, "You are a newborn.");
else if (age <= 1) JOptionPane.showMessageDialog(null, "You are an infant.");
else if (age <= 3) JOptionPane.showMessageDialog(null, "You are a toddler.");
else if (age <= 5) JOptionPane.showMessageDialog(null, "You are a preschooler.");
else if (age <= 13) JOptionPane.showMessageDialog(null, "You are a school-aged child.");
else if (age <= 19) JOptionPane.showMessageDialog(null, "You are an adolescent and a denarian.");
else if (age <= 29) JOptionPane.showMessageDialog(null, "You are a vicenarian.");
else if (age <= 39) JOptionPane.showMessageDialog(null, "You are a tricenarian.");
else if (age <= 49) JOptionPane.showMessageDialog(null, "You are a quadragenarian.");
else if (age <= 59) JOptionPane.showMessageDialog(null, "You are a quinquagenarian.");
else if (age <= 69) JOptionPane.showMessageDialog(null, "You are a sexagenarian.");
else if (age <= 79) JOptionPane.showMessageDialog(null, "You are a septuagenarian.");
else if (age <= 89) JOptionPane.showMessageDialog(null, "You are a octogenarian.");
else if (age <= 99) JOptionPane.showMessageDialog(null, "You are a nonagenarian.");
else if (age <= 109) JOptionPane.showMessageDialog(null, "You are a centenarian.");
else if (age <= 150) JOptionPane.showMessageDialog(null, "You are a supercentenarian.");
else if (age > 150)
JOptionPane.showMessageDialog(null, "Warning!The value entered is too large to be processed.", "warning", JOptionPane.WARNING_MESSAGE);
} catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input Error. "
"Please enter a number corresponding to your age only.", "error", JOptionPane.ERROR_MESSAGE);
} catch (NullPointerException e) {
JOptionPane.showMessageDialog(null, "Program canceled.");
continue;
}
uj5u.com熱心網友回復:
假設有人在彈出視窗中輸入“你好”。該JOptionPane.showInputDialog方法回傳得很好,并"Hello"交給Double.parseDouble. 這導致NumberFormatException被拋出,因此代碼切換到 catch 塊。
這個 catch 塊將列印一條關于需要輸入數字的訊息,然后.. 代碼繼續。畢竟,捕捉意味著你處理它(如果你想做一些事情但然后繼續例外程序,然后重新拋出它,或者做一些其他事情來影響控制流,比如continue;你在 NullPointer catch 塊中做的事情! )
代碼繼續進入if (age < 0)并且此代碼無法執行,因為age在這種情況下未設定。編譯器知道這一點并將拒絕編譯它。
這里的主要解決方案似乎是撰寫控制流,if (age < 0)如果用戶一開始沒有輸入數字,則該塊永遠不會“命中”。你知道怎么做——你是用一個while回圈和continue;另一個catch塊中的那個陳述句來做的。
或者,選擇一個作為默認值或占位符的年齡,以指示用戶尚未輸入內容。但這聽起來不是這種情況下的最佳解決方案 - 您不想在用戶實際輸入年齡之前處理年齡。
uj5u.com熱心網友回復:
問題是如果你的代碼輸入以下幾行代碼,變數age實際上不會被賦值:
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input Error. " "Please enter a number corresponding to your age only.", "error", JOptionPane.ERROR_MESSAGE);
}
Java 在編譯時檢查是否所有變數都已正確初始化。如果編譯器認為變數可能在下一條指令之前沒有被初始化,就會得到這個錯誤。
您可以通過簡單地初始化變數來解決這個問題:
catch (NumberFormatException e) {
age = 0D;
JOptionPane.showMessageDialog(null, "Input Error. " "Please enter a number corresponding to your age only.", "error", JOptionPane.ERROR_MESSAGE);
}
但我認為正確的方法是:
catch (NumberFormatException e) {
JOptionPane.showMessageDialog(null, "Input Error. " "Please enter a number corresponding to your age only.", "error", JOptionPane.ERROR_MESSAGE);
continue;
}
關于您的第二個問題,關于
showMessageDialog,您可以在檔案中閱讀:
public static void showMessageDialog(Component parentComponent, Object message) throws HeadlessException
Brings up an information-message dialog titled "Message".
Parameters:
* parentComponent - determines the Frame in which the dialog is displayed; if null, or if the parentComponent has no Frame, a default Frame is used
* message - the Object to display
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/458701.html
上一篇:Java例外是event.getSource().getException()之后的字串,并且很難捕獲,因為必須進行決議
