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: JFileChooser provides null string

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default JFileChooser provides null string

    I'm getting a NullPointerException in my code. I've use the debugger and traced it to the chooser.getName() line. This is returning a null value to me.

    Does anyone have an idea where I went wrong? If it makes a difference, I'm using Linux Mint and the file selected is on a cifs network folder. Perhaps Java is having some problems working with my OS? I can browse the folders fine.

    Note, I have not initialized currentFile before this point. Not sure if that would make a difference.

    private String currentFile;
    private boolean setFile() // returns true if there is an error
    {
      JFileChooser chooser = new JFileChooser();
      chooser.setFileFilter( new FileNameExtensionFilter(
        "XML files", "xml"));
     
      if (chooser.showOpenDialog(this) != JFileChooser.APPROVE_OPTION)
        return true;
     
      currentFile = chooser.getName();
      return false;
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: JFileChooser provides null string

    See the API for JFileChooser - getName is a method for Component, which must be set with setName. I presume you want the file name, or absolute path, in which case you should be calling getSelectedFile

  3. The Following User Says Thank You to copeg For This Useful Post:

    stewbond (November 1st, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    18
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: JFileChooser provides null string

    Whoops! Thanks for the easy fix.

    I was reading about this class here:
    JFileChooser (Java Platform SE 7 )
    I guess I skipped over the getSelectedFile() part of it.

Similar Threads

  1. String.split("") puts null in the first index
    By sonicjr in forum What's Wrong With My Code?
    Replies: 5
    Last Post: August 10th, 2023, 03:11 AM
  2. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  3. Retrived data is showing null string
    By uhertz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: June 6th, 2011, 03:04 AM
  4. [SOLVED] Confused about string that I think shouldn't be null
    By mjpam in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 29th, 2011, 11:08 PM
  5. JFileChooser
    By FretDancer69 in forum AWT / Java Swing
    Replies: 2
    Last Post: February 3rd, 2010, 06:35 AM