-
July 27th, 2011, 08:57 PM
#1
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.
-
July 28th, 2011, 07:22 AM
#2
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();
Can you explain what your problem is?
Norm
-
July 28th, 2011, 09:53 AM
#3
Re: Storing an absolute pathname into a String
Originally Posted by Norm
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.
-
July 28th, 2011, 09:54 AM
#4
Re: Storing an absolute pathname into a String
Norm
-
July 28th, 2011, 10:10 AM
#5
Re: Storing an absolute pathname into a String
filePath is null. There seemed to be nothing in filePath.
-
July 28th, 2011, 10:12 AM
#6
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
-
July 28th, 2011, 10:21 AM
#7
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?
-
July 28th, 2011, 10:23 AM
#8
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
-
July 28th, 2011, 10:45 AM
#9
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.
-
July 28th, 2011, 10:53 AM
#10
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
-
July 29th, 2011, 03:25 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|