Results 1 to 4 of 4
  1. #1
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Why use "null" in this situation?

    I'm learning about Java Option Panes fromJava For Complete Beginners - option panes. Line 15 is confusing me. The site explains null, but why do we need the null there? If I remove it, the compiler prints out this error, "The method showMessageDialog(Component, Object) in the type JOptionPane is not applicable for the arguments (String)". What does the error mean?

    Java Code:
    import javax.swing.JOptionPane;
    
    public class secondGUI {
    	
    	public static void main(String[] args) {
    		
    		String familyName;
    		familyName = JOptionPane.showInputDialog("Family Name");
    		
    		String firstName;
    		firstName = JOptionPane.showInputDialog("First Name");
    		
    		String fullName;
    		fullName = "Your name is " + firstName + " " + familyName;
    		JOptionPane.showMessageDialog(null, fullName); //Why do we need the null there?
    		
    		System.exit(0);
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why use "null" in this situation?

    The method expects two arguments, and so it should come as no surprise that you'll get an error if you try to call it not as written. The JOptionPane can be called as a dialog dependent on another GUI, or just by itself. When called the first way, the first argument passed into this method should be a component that is displayed on the top level Window that wishes to display the option pane. The JOptionPane will use this object to build a dialog that is "modal" to the calling window, meaning that the calling window is frozen and cannot interact with the user until the JOptionPane dialog is no longer visible. If you want to call the JOptionPane by itself and not part of a larger GUI, then you have no need to pass a component into the method, and so you pass in "nothing" or null.

  3. #3
    whileloop is offline Member
    Join Date
    Feb 2012
    Posts
    8
    Rep Power
    0

    Default Re: Why use "null" in this situation?

    Layman's terms please.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why use "null" in this situation?

    Quote Originally Posted by whileloop View Post
    Layman's terms please.
    • The method is written to expect two parameters and two only. If you try to call it with anything else, it will fail. Period.
    • The first parameter is used if you are launching the JOptionPane from another Window such as a JFrame. You're not, so pass in null.


    That's as simple as I can put it.

Similar Threads

  1. Replies: 3
    Last Post: 03-17-2012, 03:19 AM
  2. Replies: 1
    Last Post: 10-22-2011, 05:50 AM
  3. Replies: 2
    Last Post: 03-19-2010, 05:30 PM
  4. Replies: 0
    Last Post: 07-28-2009, 06:45 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •