2012-02-29 6 views
1

私はクラスのために書いたプログラムをテストしている最中に、Javaクラスを開始してエラーのように見えます。私は宿題を手伝ってもらう必要はありません。ヌルについての2つのこと、およびヌルの解析に関する1つのことを理解したいと思います。intとdoubleの解析間でNullExceptionHandlingが異なる

最初に、サンプルコードを実行すると、スイング入力ボックスの入力なしで[OK]ボタンをクリックすると、nullと思われる文字列値が格納されることがわかります。しかし、文字列がテストされたときにはnullとしてテストされません。どうしてこれなの?

第2に、サンプルコードを実行すると、スイング入力ボックスの[キャンセル]ボタンをクリックすると、テキスト「null」を含む文字列のように見える値が格納されることがわかります。これはnull値としてテストしますが、テキスト "null"を含む文字列は明らかにnullとしてテストされません。ヌル文字列値が ""であると思ったときにキャンセルボタンがテキスト "null"を含む文字列を生成するのはなぜですか?

最後に、サンプルコードを実行すると、nullとしてテストされた値がdoubleとして解析されたときにNullPointerExceptionをスローするが、intとして解析されたときにNullPointerExceptionをスローしないことがわかります。どうしてこれなの?

私の先生はこれらの質問に答えることができませんでした。彼は、サンプルプログラムの構文解析が算術的に達成されたと考えていたので、intは小数点を扱わないため動作が異なる可能性があります。それは私にはいくらか意味がありますが、そうであれば、Stringのようなnullで解析する理由は何でしょうか?キャンセルボタンによって生成されたnull?

私のサンプルコードは以下の通りです:

まず
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; 
     } 
    } 
} 

    } 

答えて

3

、あなたは私のサンプルコードを実行するときに、任意の入力なしでスイング入力ボックスに「OK」ボタンをクリックすると、その文字列の値を格納していることがわかりますnullと思われる。しかし、文字列がテストされるとき、それはnullとしてテストされません。どうしてこれなの?

空の文字列がnullと同じではないためです。テキストがないときに「OK」を押すと、あなたが提供している値として空の文字列を受け入れます。ユーザーが入力をキャンセルし

、ユーザの入力、またはnullの意味:戻り値として文書である理由です - あなたは「キャンセル」を押すと、あなたは効果的に「私は価値を提供することを拒否」、言っています

次:第二に

、あなたは私のサンプルコードを実行するときに、スイング入力ボックスに「キャンセル」ボタンをクリックすると、「テキストを含む文字列であるように思われる値を格納していることがわかりますヌル"。これはnull値としてテストしますが、テキスト "null"を含む文字列は明らかにnullとしてテストされません。ヌル文字列値が ""であると思ったときにキャンセルボタンがテキスト "null"を含む文字列を生成するのはなぜですか?

いいえ、値はヌル参照です。 null参照はではなく、 ""または "null"への参照と同じであるではありません。しかし、文字列の連結では、null参照が "null"に変換されます。"a" + null + "b"は、"anullb"となります。

Javaでの参照の仕組みを理解することは非常に重要です。参照とはオブジェクトにナビゲートする方法です。ヌル参照とは、「移動するオブジェクトがありません」という意味です。基準値を持つ変数を、住所が書かれた紙のようなものと考える場合、値がヌルの場合、紙は空白になります。

あなたは私のサンプルコードを実行するときに最後に、あなたは二重のように解析する際はnullとして試験されている値は、NullPointerExceptionがスローいることがわかりますが、彼らは、彼らがintとして解析され、NullPointerExceptionがスローされません。どうしてこれなの?

APIの不一致のようです。 Integer.parseIntは代わりにNumberFormatExceptionをスローします。どちらも文書化されているように行動している。

+0

'Double.parseDouble'は' String'の 'trim()'関数を使用している 'FloatingDecimal.readJavaFormatString'を呼び出しています。つまり、ヌル文字列を渡すと、NPEが得られます。 [Double](http://www.docjar.com/html/api/java/lang/Double.java.html)、[FloatingDecimal](http://www.docjar.com/html/api/sun/misc /FloatingDecimal.java.html) – Deco

+0

ありがとうございました。私は空の文字列がnull文字列であると考え、それゆえにnull値だと考えました。 Javaは多くの点でC#と非常によく似ているので、このコンセプトはC#に転送されますか? – Erik

+0

@ErikBollinger:それは大部分ですが、空の文字列への参照とはまったく同じではありません。空の文字列は、文字を持たない通常の文字列です。長さなどを問い合わせることができます。 –

0

私はあなたの問題はここに常駐推測:

JOptionPane.showMessageDialog(null, "Double Tester\n'" + answer + "'\nIt appe... 
                 ^^^^^^ 

answerは、(ユーザーがCancelを押す)nullであれば、メッセージのように出てくる:

ダブルテスター
ヌル
それAPPE ...

しかしユーザーは、このように与えて、空の入力フィールド、answer参照空のString"")とOkを押した場合:

ダブルテスター

をそれは

... APPE

nullという参照("null")の文字列値と空の文字列が混乱しているという問題があります("")。

関連する問題