Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: NullPointerException when canceling JOptionPane.showInputDialog

  1. #1
    Junior Member
    Join Date
    Jun 2010
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question NullPointerException when canceling JOptionPane.showInputDialog

    Hello folks,

    Not sure what the problem is here. When I run this code it creates directories just fine. If I hit the "cancel" button, however, it creates a directory called "null". When I attempt to check the string for null, I get a NullPointerException. Any help is greatly appreciated.

    	public void makeTables() throws IOException{
    		String dirPath = "data/tendtables/";
    		String tname = (String)JOptionPane.showInputDialog(
    				"Enter the name of the new strain:");
     
    		// Canceled?
    		if (tname.equals(null))
    			return;
     
    		// Check if directory already exists
    		File tendTableDir = new File(dirPath + tname);
    		if (tendTableDir.exists()){
    			JOptionPane.showMessageDialog(null, "Strain already created!");
    		}else{
    				// Create the directory
    				tendTableDir.mkdir();


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: NullPointerException when canceling JOptionPane.showInputDialog

    If tname is null, then there is no object with an equals() method to dereference. Hence a NPE
    The way to test if an object reference such as tname is null it to use ==
    For example: if (tname == null) ...

  3. The Following 2 Users Say Thank You to Norm For This Useful Post:

    Json (July 2nd, 2010), Shaddam (July 1st, 2010)

  4. #3
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default Re: NullPointerException when canceling JOptionPane.showInputDialog

    Or to try/catch them NPE's. Try/catching is good practice, youll need to be adept at it later anyways . Not to mention the fact that teachers love error-handling.

Similar Threads

  1. Using Icons in JOptionPane
    By Jchang504 in forum AWT / Java Swing
    Replies: 3
    Last Post: September 19th, 2014, 02:28 PM
  2. [SOLVED] Scroll down in JOptionPane and window problems.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 60
    Last Post: June 16th, 2010, 12:04 PM
  3. Return result from JOptionPane to JFrame
    By cselic in forum AWT / Java Swing
    Replies: 7
    Last Post: May 13th, 2010, 01:17 PM
  4. showInputDialog method
    By chronoz13 in forum AWT / Java Swing
    Replies: 2
    Last Post: November 22nd, 2009, 05:47 PM
  5. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 09:17 AM