0

I'm actually trying to create a file with a specific file name. The problem is, that the filename contains german umlauts, so I will always get a NullPointerException.

The code looks like this:

File f = new File("/Volume/dir1/dir2/dirWithUmlauts");
File[] files = f.listFiles(); // NullPointerException

I tested the same on Windows and it works. I have no idea why it does not work on Mac.

tshepang
  • 12,111
  • 21
  • 91
  • 136
user2048767
  • 135
  • 10
  • In the future, you may want to specify "OS X" in your question title or description. It will make what you want more obvious. I'll read the question in depth now. – Russell Uhl Jul 03 '13 at 19:58
  • That works fine by me. Could you give us the stacktrace? Are you sure the path is correct? – LaurentG Jul 03 '13 at 20:00
  • 1
    It's impossible to get `NPE` after variable was created by `new` operator! It is 100% not null – Ilya Jul 03 '13 at 20:01
  • Which version of `java` ? – Ilya Jul 03 '13 at 20:03
  • I get it after ´f.listFiles();´ – user2048767 Jul 03 '13 at 20:03
  • java version "1.7.0_15" – user2048767 Jul 03 '13 at 20:04
  • llya: +1 great! @user2048767: the stack trace points exactly to this line AND the code of the line is 100% like you posted THEN try to recompile! probably you've added or removed one or more lines from the code after compile and debug... – Marvin Emil Brach Jul 03 '13 at 20:12
  • 1
    is it possible the NPE is happening on the NEXT line, presumably where you access the `files` variable? if so, See my updated answer below – Russell Uhl Jul 03 '13 at 20:17
  • possible duplicate of [File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)](http://stackoverflow.com/questions/3610013/file-listfiles-mangles-unicode-names-with-jdk-6-unicode-normalization-issues) – Edward Thomson Jul 03 '13 at 22:06

2 Answers2

0

I don't know how Mac OS will treat umlauts in the file system. But if the directory could be handled correctly out of the JVM it has to work in it, too.

So try to list the name of the directory trough a listFiles() one level above:

File d = new File("/Volume/dir1/dir2/"); 
for( File f : d.listFiles()) System.out.println(f.getName());

So you will see how the String has to look like for opening the file (also it really exists and is accessible from the VM);


SOLUTION:

File.listFiles() mangles unicode names with JDK 6 (Unicode Normalization issues)

Community
  • 1
  • 1
Marvin Emil Brach
  • 3,984
  • 1
  • 32
  • 62
  • since the asker did not mention anything about a virtual machine, I will assume you meant JVM. If this is correct, you may want to edit your post to say as such to avoid confusion. other than that, nice answer! – Russell Uhl Jul 03 '13 at 20:07
  • @user2048767 http://stackoverflow.com/questions/3610013/file-listfiles-mangles-unicode-names-with-jdk-6-unicode-normalization-issues its about Java 1.6, but I think it will help you, too ;) – Marvin Emil Brach Jul 03 '13 at 20:25
  • @RussellUhl thanks for the hint, for me it was clear (thou shalt not have another VM then JVM :P) ... and thanks ;) – Marvin Emil Brach Jul 03 '13 at 20:27
  • @user2048767 please acceppt the answer if it was helpful – Marvin Emil Brach Jul 04 '13 at 10:26
0

You say you are trying to create a file, but your code lists the contents of a directory. Are you (unintentionally) listing the "folder contents" of a FILE? According to http://docs.oracle.com/javase/7/docs/api/java/io/File.html: "If this abstract pathname does not denote a directory, then this method returns null."

So if, as I suspect, you are attempting to list the contents of a non-directory, your File[] array is null. Therefore, when you attempt to use it, your code will explode with an NPE

Russell Uhl
  • 4,181
  • 2
  • 18
  • 28
  • @MarvinEmilBrach that wasn't precisely what I was going for, but that could be it too. What I meant was that he was trying to view the contents of a directory, except the directory was an actual file. I don't know how java would handle such an event. – Russell Uhl Jul 03 '13 at 20:09
  • http://docs.oracle.com/javase/6/docs/api/java/io/File.html#listFiles%28%29: **Returns null if this abstract pathname does not denote a directory** ... nevertheless: llya is rigth, there is something messed up with the code (in the IDE or just here) – Marvin Emil Brach Jul 03 '13 at 20:18
  • 1
    @MarvinEmilBrach: read carefully. My argument applies only if he is then trying to use the File[] array he created AND the "directory" is not, in fact, a directory. – Russell Uhl Jul 03 '13 at 20:20
  • I misunderstood the *your code list the content* and thought you meant the listFiles() command – Marvin Emil Brach Jul 03 '13 at 20:32