Java 分析int和double之间的不同NullExceptionHandling

Java 分析int和double之间的不同NullExceptionHandling,java,parsing,nullpointerexception,double,int,Java,Parsing,Nullpointerexception,Double,Int,我正在学习一门初级Java课程,在测试我为该课程编写的程序时,无意中发现了一个看似错误的问题。我不需要帮助做家庭作业。我想了解关于nulls的两件事,以及关于解析nulls的一件事 首先,当您运行我的示例代码时,您将看到在没有任何输入的情况下单击swing输入框上的“OK”按钮会存储一个看起来为空的字符串值。但是,当测试字符串时,它不会作为null进行测试。为什么会这样 其次,当您运行我的示例代码时,您将看到单击swing输入框上的“Cancel”按钮存储的值似乎是一个包含文本“null”的字符

我正在学习一门初级Java课程,在测试我为该课程编写的程序时,无意中发现了一个看似错误的问题。我不需要帮助做家庭作业。我想了解关于nulls的两件事,以及关于解析nulls的一件事

首先,当您运行我的示例代码时,您将看到在没有任何输入的情况下单击swing输入框上的“OK”按钮会存储一个看起来为空的字符串值。但是,当测试字符串时,它不会作为null进行测试。为什么会这样

其次,当您运行我的示例代码时,您将看到单击swing输入框上的“Cancel”按钮存储的值似乎是一个包含文本“null”的字符串。这确实测试为空值,但包含文本“null”的字符串显然不会测试为空值。当我认为空字符串值为“”时,为什么“取消”按钮生成文本为“null”的字符串

最后,当您运行我的示例代码时,您将看到测试为null的值在解析为double时抛出NullPointerException,但在解析为int时不会抛出NullPointerException。这是为什么

我的老师不能回答这些问题。他认为示例程序中的解析是通过算术完成的,因此int的行为可能会有所不同,因为它们不处理小数点。这对我来说有些道理,但如果是这样,为什么它们会用类似null的字符串answer=“”)进行不同的解析;“取消”按钮生成的空值是多少

我的示例代码如下:

    import javax.swing.*; 
    public class NullTest {

public static void main(String[] args) {
    //Demonstrate behavior with doubles.
    throwingDoubles();

    //Demonstrate behavior with integers.
    throwingInts();     
}

public static void throwingDoubles()
{

    //Loops three times so you can test each option.
    for (int i = 0; i < 3; i++)
    {
        JOptionPane.showMessageDialog(null, "Double Tester\nFirst time through click ok without entering text.\nSecond time through click cancel.\nFinally type in null to prove that this string is not treated as a null value.");
        String answer = JOptionPane.showInputDialog(null, "I would think 'answer' would be null if you click ok without entering anything.");
        JOptionPane.showMessageDialog(null, "Double Tester\n'" + answer + "'\nIt appears null here if you don't enter anything, but as a string if you click cancel");

        if (answer==null)
        {
            JOptionPane.showMessageDialog(null, "Double Tester\nTested null");
            JOptionPane.showMessageDialog(null, "Double Tester\n'" + answer + "'\nIt appears the same here.");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Double Tester\nDid not test null");
            JOptionPane.showMessageDialog(null, "Double Tester\n'" + answer + "'\nIt appears the same here.");
        }

        try
        {
            double varDoub = Double.parseDouble(answer);    
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Double Tester\nThis threw a NumberFormatException");
        }
        catch(NullPointerException e)
        {
            JOptionPane.showMessageDialog(null, "Double Tester\nThis threw a NullPointerException");
        }
        //An early escape clause.
        if (JOptionPane.showConfirmDialog(null, "Run the loop again?")!=JOptionPane.OK_OPTION)
        {
            break;
        }
    }
}


public static void throwingInts()
{

    //Loops three times so you can test each option.
    for (int i = 0; i < 3; i++)
    {
        JOptionPane.showMessageDialog(null, "Int Tester\nFirst time through click ok without entering text.\nSecond time through click cancel.\nFinally type in null to prove that this string is not treated as a null value.");
        String answer = JOptionPane.showInputDialog(null, "Int Tester\nI would think 'answer' would be null if you click ok without entering anything.");
        JOptionPane.showMessageDialog(null, "Int Tester\n'" + answer + "'\nIt appears null here if you don't enter anything, but as a string if you click cancel");

        if (answer==null)
        {
            JOptionPane.showMessageDialog(null, "Int Tester\nTested null");
            JOptionPane.showMessageDialog(null, "Int Tester\n'" + answer + "'\nIt appears the same here.");
        }
        else
        {
            JOptionPane.showMessageDialog(null, "Int Tester\nDid not test null");
            JOptionPane.showMessageDialog(null, "Int Tester\n'" + answer + "'\nIt appears the same here.");
        }

        try
        {
            int varDoub = Integer.parseInt(answer); 
        }
        catch(NumberFormatException e)
        {
            JOptionPane.showMessageDialog(null, "Int Tester\nThis threw a NumberFormatException");
        }
        catch(NullPointerException e)
        {
            JOptionPane.showMessageDialog(null, "Int Tester\nWe never see this code. Why not?");
        }
        //An early escape clause.
        if (JOptionPane.showConfirmDialog(null, "Run the loop again?")!=JOptionPane.OK_OPTION)
        {
            break;
        }
    }
 }

    }
import javax.swing.*;
公共类空值测试{
公共静态void main(字符串[]args){
//用双打示范行为。
投掷双打();
//用整数演示行为。
投掷物();
}
公共静态void throwingsdouble()
{
//循环三次,以便可以测试每个选项。
对于(int i=0;i<3;i++)
{
JOptionPane.showMessageDialog(null,“双击测试仪\n第一次单击确定而不输入文本。\n第二次单击取消。\n最后键入null以证明此字符串未被视为空值。”);
String answer=JOptionPane.showInputDialog(null,“如果单击ok而不输入任何内容,我认为‘answer’将为null”);
JOptionPane.showMessageDialog(null,“Double Tester\n'”+answer+“\n如果您不输入任何内容,它将在此处显示为null,但如果您单击“取消”,它将显示为字符串);
如果(答案==null)
{
showMessageDialog(空,“双测试仪\n测试空”);
showMessageDialog(null,“双测试仪\n'”+answer+“\n此处显示相同”);
}
其他的
{
showMessageDialog(null,“双测试仪\nID不测试null”);
showMessageDialog(null,“双测试仪\n'”+answer+“\n此处显示相同”);
}
尝试
{
double varDoub=double.parseDouble(答案);
}
捕获(数字格式)
{
showMessageDialog(null,“双测试仪\n这引发了NumberFormatException”);
}
捕获(NullPointerException e)
{
showMessageDialog(null,“双测试仪\n这引发了NullPointerException”);
}
//提前免责条款。
if(JOptionPane.showConfirmDialog(null,“再次运行循环?”)!=JOptionPane.OK\u选项)
{
打破
}
}
}
公共静态void throwingInts()
{
//循环三次,以便可以测试每个选项。
对于(int i=0;i<3;i++)
{
JOptionPane.showMessageDialog(null,“Int Tester\n第一次单击确定而不输入文本。\n第二次单击取消。\n最后键入null以证明此字符串未被视为空值。”);
String answer=JOptionPane.showInputDialog(null,“Int Tester\n如果您单击ok而不输入任何内容,我会认为‘answer’将为null。”);
JOptionPane.showMessageDialog(null,“Int Tester\n'”+answer+“\n如果您不输入任何内容,它在此处显示为null,但如果您单击“取消”,它将显示为字符串);
如果(答案==null)
{
showMessageDialog(null,“Int Tester\nTested null”);
showMessageDialog(null,“Int Tester\n'”+answer+“\n此处显示相同”);
}
其他的
{
showMessageDialog(null,“Int Tester\nDid not test null”);
showMessageDialog(null,“Int Tester\n'”+answer+“\n此处显示相同”);
}
尝试
{
int varDoub=Integer.parseInt(答案);
}
捕获(数字格式)
{
showMessageDialog(null,“Int Tester\n这引发了NumberFormatException”);
}
捕获(NullPointerException e)
{
showMessageDialog(null,“Int Tester\n我们从未看到此代码。为什么不?”);
}
//提前免责条款。
if(JOptionPane.showConfirmDialog(null,“再次运行循环?”)!=JOptionPane.OK\u选项)
{
打破
}
}
}
}
首先,当您运行我的示例代码时,您将看到在没有任何输入的情况下单击swing输入框上的“OK”按钮会存储一个看起来为空的字符串值。但是,当测试字符串时,它不会作为null进行测试。为什么会这样

因为空字符串与空字符串不同。当您在没有文本的情况下点击“OK”时,您接受一个空字符串作为您提供的值。当您按下“取消”键时,实际上是在说“我拒绝提供值”-这就是返回值记录为以下内容的原因:

用户的输入,或null表示用户取消了输入

下一步:

其次,当您运行我的示例代码时,您将看到单击
JOptionPane.showMessageDialog(null, "Double Tester\n'" + answer + "'\nIt appe...
                                                         ^^^^^^