• Post Reply Bookmark Topic Watch Topic
  • New Topic

Files  RSS feed

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys I keep getting a nullPointerException and I can't figure out why?

my directory contains 4 files and 3 directories




then I changed the code a little to make sure the file is a directory so maybe I wouldn't get a nullPointer exception it worked no exceptions but the only problem is that it does not actually print the files




any ideas how I can get this code working to actually print the file names?

thanks
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a way to list all the directories in my workspace

but I still need to figure out a way to print the files in those directories

 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok guys I figured out the solution,feedback is more than welcome


thanks
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done for figuring it out. Show us your solution and we will give you some feedback.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry forgot to post the solution

 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't like your making both methods static. Yes, you have to start off in a static method (main) but you shou‍ld get out of a static context as soon as possible. Since you are not creating any objects, that isn't an object‑oriented approach.

In the case of listing files, you can argue that you you won't notice the difference between the results from an object‑oriented (=OO) solution and a procedural solution, but you shou‍ld probably get into the habit of writing OO as soon as possible.
 
Adam Chalkley
Ranch Hand
Posts: 518
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks Campbell great tips, I'll keep them in mind =)
 
Carey Brown
Saloon Keeper
Posts: 3329
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a slight reorganization of your solution but this will handle the case where the parameter 'f' could be a file or a directory.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!