当前位置: 首页 > 知识库问答 >
问题:

由于试图捕获多个异常,出现了错误“局部变量年龄可能尚未初始化”

史谦
2023-03-14

错误“局部变量 age 可能尚未初始化”是由于尝试捕获多个异常而发生的。

你好。在前一个问题得到解决之后,我决定对程序做一个小小的改动,使用JOptionPane将控制台中的打印转换成一个简单的GUI,这时出现了一个新的错误。我试图捕捉两个名为NumberFormatException和NullPointerException的特定异常。NumberFormatException是在JOptionPane.showMessageDialog的空白处输入数字以外的字符的结果,因为该变量已被声明为double。单击X符号或取消按钮会导致NullPointerException。为了捕捉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,“程序已取消。”)不存在,则不会发生错误。

顺便说一下,除了主要问题之外,你能在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);
        }
    }
}

共有3个答案

戎桐
2023-03-14

问题是,如果您的代码输入以下代码行,则变量 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;
    }

关于您的第二个问题,关于show MessageDialog,您可以在留档中阅读:

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
云英才
2023-03-14

假设有人在弹出窗口中输入“hello”。< code > joptionpane . showinputdialog 方法返回的结果很好,并且< code >“Hello”被传递给< code>Double.parseDouble。这导致引发< code > NumberFormatException ,因此代码切换到catch块。

此catch块将打印一条关于需要输入数字的消息,然后..代码继续。毕竟,捕获意味着您要处理它(如果您想做一些事情,但随后继续执行异常过程,然后重新抛出它,或者做一些其他事情来影响控制流,例如继续;,这是您在NullPointer捕获块中做的!)

代码继续到<Code>if(年龄

这里的主要解决方案似乎是编写控制流,使得 if(age)

或者,选择一个用作默认值或占位符的期限,以指示用户尚未输入内容。但这听起来不像是这种情况的最佳解决方案 - 在用户实际输入年龄之前,您不想处理年龄。

裴俊能
2023-03-14

尝试将所有年龄验证登录移到Try块中,其中年龄,变量是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;
}
            
 类似资料:
  • 问题内容: UI类在View中,导入已完成,但是在最后一个表达式中我得到了错误。 我是Java的入门者,但我不明白为什么我不允许这样使用它。 问题答案: 如果要在Java方法中声明变量/对象,则需要对其进行初始化。 简单来说 在您的情况下,它是一个正在访问方法的对象,因此,如果不初始化它,就像 它会给你一个NULL指针异常。 希望能帮助到你。

  • 问题内容: 当我尝试编译时: 我得到这些错误: 在我看来,我在方法的顶部初始化了它们。怎么了 问题答案: 你声明了它们,但没有初始化它们。初始化它们是将它们设置为等于一个值: 因为未初始化变量,但在循环中增加了变量(例如),因此会收到错误消息。 Java原语具有默认值,但如下一位用户所述 当声明为类成员时,它们的默认值为零。局部变量没有默认值

  • 我不知道这段代码有什么问题,也不知道为什么会出现错误: 变量isPrime可能尚未初始化 这是完整的代码:

  • 我创建了包含构造函数和toString方法的类主管。但是,当我试图打印数组的索引时,出现了一个错误,“变量svArray可能尚未初始化。我该如何解决这个问题?”?

  • 问题内容: 我有一个方法创建一个,另一个方法更改字符串 我的编译器说它“可能尚未初始化”。 有人可以解释吗? 问题答案: 变量可能尚未初始化 在内部定义方法时,必须在其中初始化程序的每个变量中必须先使用一个值的地方。 同样重要的是,您的代码将永远无法正常运行,因为Java中的字符串是不可变的,因此您无法编辑字符串,因此应更改方法。 我将您的代码更改为类似的内容,但是我认为您的编辑方法应该做另一件事

  • 问题内容: 我有一些具有这种结构的代码: 我有很多方法在catch块中具有相同的代码,因此我想将其提取到一种方法中,以便节省一些行。我的问题是,如果这样做,则会收到编译器错误“本地变量o可能尚未初始化”。 有什么解决方法吗? 问题答案: 您需要先初始化局部变量,然后才能按如下方式使用它们 除非使用未初始化的局部变量,否则编译失败