I am taking a beginning Java class and stumbled on what seems like an error while testing a program I wrote for class. I do not need help with the homework assignment. I would like to understand two things about nulls and one thing about parsing nulls.
Firstly, when you run my example code you will see that clicking the ‘OK’ button on a swing input box without any input stores a string value that appears to be a null. However when the string is tested it does not test as a null. Why is this?
Secondly, when you run my example code you will see that clicking the ‘Cancel’ button on a swing input box stores a value that appears to be a string containing the text “null”. This does test as a null value, but a string containing the text “null” obviously will not test as null. Why does the cancel button generate a string with the text “null” when I thought a null string value was “”?
Finally, when you run my example code you will see that values that are tested as null throw a NullPointerException when parsed as a double, but they do not throw a NullPointerException when they are parsed as an int. Why is this?
My teacher could not answer these questions. He thought that the parsing in the sample program was accomplished arithmetically, so the ints might behave differently because they do not deal with decimal points. That makes some sense to me, but if that is the case why do they parse differently with a null like String answer = “”; and the null generated by the cancel button?
My sample code is below:
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;
}
}
}
}
Because an empty string isn’t the same as a null. When you hit “OK” when there’s no text, you’re accepting an empty string as the value you’re providing. When you press “Cancel” you’re effectively saying, “I refuse to provide a value” – which is why the return value is documented as:
Next:
No, it stores a value that’s a null reference. A null reference is not the same as a reference to either “” or “null”. But in string concatenation, a null reference is converted to “null”:
"a" + null + "b"will end up as"anullb".It’s very important that you understand how references work in Java. A reference is a way of navigating to an object, effectively… and a null reference is a way of saying, “There’s no object to navigate to.” If you think of a variable with a reference value as being like a sheet of paper with a street address written on it, if the value is null the sheet of paper is blank.
Looks like it’s just a bit of inconsistency in the API.
Integer.parseIntthrows aNumberFormatExceptioninstead. Both are behaving as documented though.