0

I want to do that:

JFileChooser fileChooser = new JFileChooser();
fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir" + "\\datos_medidas")));

But I recieve a NullPointerException.

All I want to do is to put the current directory path on a folder that is in the user.dir folder, I have a folder in user.dir and I want to save my files in that folder, but I don't know how to do it.

I can't use a "literal path" I need a relative path because this application is going to work on all windows versions and I can't use literal paths.

Imrik
  • 674
  • 2
  • 14
  • 32

1 Answers1

1

I think that the reason is pretty obvious. Examine your code:

System.getProperty("user.dir" + "\\datos_medidas")

You try to retrieve system property that does not exist. Instead you should retrieve system property user.dir that represents the file system path and create File object that uses this path as a parent:

new File(System.getProperty("user.dir"), "datos_medidas"))
AlexR
  • 114,158
  • 16
  • 130
  • 208
  • can you tell me how to use "MyDocuments" on setCurrentDirectory? – Imrik Feb 26 '15 at 12:09
  • It seems that I do not understand your question. What is the difference between "datos_medias" and "My Documents"? – AlexR Feb 26 '15 at 12:13
  • the difference is that "datos_medias" is a folder that I make, so it has always the same name and the same path, but "My Documents" folder could be "Documents" on english windows vista but in spanish it's called "Mis Documentos", I ask you if you know how to set the currentDirectory path working always in "My Documents" windows folder, that it's created by operating system but in other languages is called different. I sopouse that I cannot use ("user.home", "Documents") because it doesn't work in spanish version, isn't it? – Imrik Feb 26 '15 at 12:17
  • It is completely different question. I have a answer but I do not want to write garbage here, so, ask this question and I will reply you. – AlexR Feb 26 '15 at 12:22
  • I need 90 minutes to post other question... So i have to wait... (or you can post your answer here, by email... what you want) – Imrik Feb 26 '15 at 12:27
  • As you mentioned name of this folder depends on the UI language of OS. There is not crossplatform way to retrieve this. Windows does not have environement variables that hold this information. But this information is written in registry. Please take a look on this discussion: http://stackoverflow.com/questions/2000638/whats-the-environment-variable-for-the-path-to-the-desktop for details. Registry access is different challenge in java. I recommend you to either run `reg` utility from command line or use one of available libraries, e.g. one that I wrote: https://github.com/alexradzin/RegInJ – AlexR Feb 26 '15 at 12:34