Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7573237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T16:07:54+00:00 2026-05-30T16:07:54+00:00

I am taking a beginning Java class and stumbled on what seems like an

  • 0

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

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T16:07:55+00:00Added an answer on May 30, 2026 at 4:07 pm

    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?

    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:

    user’s input, or null meaning the user canceled the input

    Next:

    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 “”?

    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.

    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?

    Looks like it’s just a bit of inconsistency in the API. Integer.parseInt throws a NumberFormatException instead. Both are behaving as documented though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am taking a beginning C++ class, and would like to convert letters between
I'm taking a beginning Java class and an assignment requires that I write a
I'm a Java novice taking a beginning programming class and I'm trying to finish
Taking beginning Data Structures in C# class, trying to make a searchable arraylist of
I am taking a database class and at the beginning of the lab section
Taking the jQuery framework for example, if you run code like this: $(document).ready(function init()
I'm beginning my journey with JavaScript and programming in general. Not having many developers
I need do find a cycle beginning and ending at given point. It is
It seems like a lot of people struggle with date/time issues in PHP, and
I'm taking over an application. It has no testing. I'm looking for the bare

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.