Results 1 to 4 of 4
- 04-22-2012, 06:20 AM #1
Member
- Join Date
- Feb 2012
- Posts
- 8
- Rep Power
- 0
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); } }
-
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.
- 04-22-2012, 07:01 AM #3
Member
- Join Date
- Feb 2012
- Posts
- 8
- Rep Power
- 0
Re: Why use "null" in this situation?
Layman's terms please.
-
Re: Why use "null" in this situation?
- 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
-
scanning lines from notepad, by creating a loop "while(input_line=NULL) {"
By noobplus in forum New To JavaReplies: 3Last Post: 03-17-2012, 04:19 AM -
request.getParameter("Login") returns null in servlets
By pink123 in forum Java ServletReplies: 1Last Post: 10-22-2011, 06:50 AM -
JOptionPane.showMessageDialog(null,"Etc Etc"); - What does null actually do?
By markious in forum New To JavaReplies: 2Last Post: 03-19-2010, 06:30 PM -
jList Issues - getSelectedValue() Keeps returning "null"
By MoobKeeng in forum New To JavaReplies: 0Last Post: 07-28-2009, 07:45 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 08:35 AM
Bookmarks