Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 1.7 nio question(s)  RSS feed

 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying out Java 1.7 and am trying to create a temporary directory using java.nio.file.Files.createTempDirectory(Path, FileAttribute<?>...) however I can't figure out the FileAttribute part of it. I get what a file attribute is at a high level, but I can't put null in it because it ends up throwing a NullPointerException. Where do I get/create FileAttributes to pass into it?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadoc of that class says that the FileAttribute param is optional and so I wouldn't expect a NullPointerException for that param. I gave it try locally:



with this output:



So works fine for me. Can you post the entire exception stacktrace and also the output of



command from where you are running this program?
 
Mohamed Sanaulla
Bartender
Posts: 3169
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get away without providing the 2nd Parameter, something like: Files.createTempDirectory(delMe);
 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the code I tried:


Here is the output:


If I remove the null from the createTempDirectory, it works without issue.

Here is my java version info:


Although this fixes my immediate issue, it doesn't answer my underlying question: Are there any built-in FileAttribute objects that I can use in this method (and other similar methods)? If so, where are they?
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which operating system are you on? Looking at the stacktrace, I do see a potential for a NPE in the TempFileHelper but that depends on a specific condition.

Although this fixes my immediate issue, it doesn't answer my underlying question: Are there any built-in FileAttribute objects that I can use in this method (and other similar methods)? If so, where are they?

Yes, take a look at http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/PosixFilePermissions.html#asFileAttribute%28java.util.Set%29 and PosixFilePermission for examples.
 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:Which operating system are you on? Looking at the stacktrace, I do see a potential for a NPE in the TempFileHelper but that depends on a specific condition.

Although this fixes my immediate issue, it doesn't answer my underlying question: Are there any built-in FileAttribute objects that I can use in this method (and other similar methods)? If so, where are they?

Yes, take a look at http://docs.oracle.com/javase/7/docs/api/java/nio/file/attribute/PosixFilePermissions.html#asFileAttribute%28java.util.Set%29 and PosixFilePermission for examples.

I am using Linux, although I'd prefer to write this so it is OS-agnostic if reasonably possible.

Line 97 in java.nio.file.TempFileHelper:

If I leave the argument blank, then attrs is an empty array, whereas if I explicitly pass a null value, then attrs is null and causes a NPE.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I consider this NullPointerException a bug in the Java 7 implementation for a couple of reasons:

1) Unlike some other classes, the javadoc of this class doesn't say that the FileAttribute param cannot be null.
2) It throws a NullPointerException if I change my code from:



to



Although the APIs I use there in those 2 cases are different, I wouldn't expect it to throw a NPE for the second param, in one case while not in other.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, I forgot that the Java 7 tutorial has a section about FileAttribute http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html. That might answer your questions about it.
 
Michael Golightly
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:I consider this NullPointerException a bug in the Java 7 implementation for a couple of reasons:

1) Unlike some other classes, the javadoc of this class doesn't say that the FileAttribute param cannot be null.
2) It throws a NullPointerException if I change my code from:



to



Although the APIs I use there in those 2 cases are different, I wouldn't expect it to throw a NPE for the second param, in one case while not in other.

I agree that this is a bug, and an easy one to fix too.

Thanks for your help everyone!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hm, I think they just need to fix the documentation to say that yes, it throws NPE.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:Although the APIs I use there in those 2 cases are different, I wouldn't expect it to throw a NPE for the second param, in one case while not in other.

But if you look again at the APIS, it does clearly say "may be null" for the prefix and suffix parameters, which are the ones you're using when you successfully passing in null. It's only when you pass in a null atts parameter that it gives you problems. So they're consistent about telling you when null is allowed. Moral: don't use null unless it's specifically allowed.
 
Rob Spoor
Sheriff
Posts: 20893
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:I consider this NullPointerException a bug in the Java 7 implementation for a couple of reasons:

1) Unlike some other classes, the javadoc of this class doesn't say that the FileAttribute param cannot be null.
2) It throws a NullPointerException if I change my code from:



to



Although the APIs I use there in those 2 cases are different, I wouldn't expect it to throw a NPE for the second param, in one case while not in other.

There's a big difference between these two calls. In the first, the null is not the FileAttribute<?>... attrs argument but the String prefix argument. In other words, you are passing an empty array for attrs.
 
Jaikiran Pai
Sheriff
Posts: 10447
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:
There's a big difference between these two calls. In the first, the null is not the FileAttribute<?>... attrs argument but the String prefix argument. In other words, you are passing an empty array for attrs.


@Rob, good catch! FWIW, changing it to:


throws a NPE. So at least it's "consistent".

@Mike, I guess you are right about not passing null when that param javadoc doesn't specifically say anything about null value. Perhaps it's just a language problem - the mention about the param being optional made me think I could get away by passing null values too.

However, I personally prefer that those APIs treat a null FileAttribute param as an empty array just like they do with Class.getMethod(name, paramTypes) method:

If parameterTypes is null, it is treated as if it were an empty array.


That way while calling those NIO APIs I won't have to worry about what the value of FileAttribute type while invoking those APIs. So I can just do:



But that's just a personal preference more than anything else. FWIW, I have created a bug in their issue tracker pointing back to this thread. It's pending evaluation and isn't public yet.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jaikiran Pai wrote:@Rob, good catch!

Hmm, I thought you'd already spotted this; it was in my comments as well, the fact that you were dealing with different params. I thought you just wanted the handling to be more consistent.

Jaikiran Pai wrote:However, I personally prefer that those APIs treat a null FileAttribute param as an empty array just like they do with Class.getMethod(name, paramTypes) method:

If parameterTypes is null, it is treated as if it were an empty array.


That way while calling those NIO APIs I won't have to worry about what the value of FileAttribute type while invoking those APIs. So I can just do:



But that's just a personal preference more than anything else. FWIW, I have created a bug in their issue tracker pointing back to this thread. It's pending evaluation and isn't public yet.

To me, handling null as a special case seems unnecessary; why would you ever need to pass in null? Documenting that behavior just gives you two ways to do the same thing, requiring extra documentation. I would simply consider getFileAttributesFromSomewhereAndDontWorryAboutTheValue() to be defective if it ever returned null. Who writes that sort of code?

I'm guessing that since Class is a much older class, and they just retrofitted the parameter Class[] parameterTypes into Class... parameterTypes, they were maintaining the contract for the original code, which allowed null because, well, Java design was all over the map back then. But in modern code like Paths and Files there's really no good reason to ever use null when an empty array is safer, so they just stuck to that.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!