CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Storing an absolute pathname into a String

    How do I get the absolute pathname of a file to be stored in an array of a String? I will be using a JFileChoose to choose a file, then the absolute pathname of the file will be stored in an array of a String.

    I tried to do this with below but it won't work:

    Code:
    int returnVal = fc.showOpenDialog(mainFrame.this);
    String[] filePath;
    int x = 0;
    String name;
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = fc.getSelectedFile();
    
                    jTextArea1.append("Opening: " + file.getName() + "." + newline + file.getPath() + newline);
                    name = new String(file.getName());
                    
                    filePath[x] = file.getAbsolutePath();
                    
                    listModel.addElement(name);
    It seems like I am unable to get the absolute path and put into the String. Is there any ways to do this? Thanks in advance.

  2. #2
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Storing an absolute pathname into a String

    I am unable to get the absolute path and put into the String
    I see you doing exactly that in your code: filePath[x] = file.getAbsolutePath();
    it won't work:
    Can you explain what your problem is?
    Norm

  3. #3
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Storing an absolute pathname into a String

    Quote Originally Posted by Norm View Post
    I see you doing exactly that in your code: filePath[x] = file.getAbsolutePath();

    Can you explain what your problem is?
    I am getting NullPointerException. It seems like there is a problem with the parsing of values.

  4. #4
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Storing an absolute pathname into a String

    What variable is null?
    Norm

  5. #5
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Storing an absolute pathname into a String

    filePath is null. There seemed to be nothing in filePath.

  6. #6
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Storing an absolute pathname into a String

    You have defined a variable to point to an array of Strings but you have not defined that array.
    String[] anArray = new String[30]; // define an array of 30 elements
    Norm

  7. #7
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Storing an absolute pathname into a String

    hahaha! Was trying to solve the problem for so long... and that was the only problem. sigh... Well, thanks for your help!

    Another question, is there any ways to create a dynamic array to store the Strings?

  8. #8
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Storing an absolute pathname into a String

    What is a "dynamic" array?
    Perhaps an ArrayList would be better if you don't know how many elements are going to be added.
    Norm

  9. #9
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Storing an absolute pathname into a String

    Oh... yea. I'm gonna try ArrayList because I don't know how many elements are there going to be. That is just what I needed.

    Is it possible to add String into ArrayList and then take it out as String on the ArrayList? Cause when I am trying to get the element in the ArrayList, it returns as an Object and not as String.

  10. #10
    Join Date
    Jun 1999
    Location
    Eastern Florida
    Posts
    3,877

    Re: Storing an absolute pathname into a String

    Look at using generics. Go to this site and find Generics:
    http://download.oracle.com/javase/tu...ybigindex.html
    Norm

  11. #11
    Join Date
    Jul 2011
    Location
    .NET 3.5
    Posts
    40

    Re: Storing an absolute pathname into a String

    Thanks for the info! Got it working very well now. Thanks again!

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured