aspose file tools*
The moose likes I/O and Streams and the fly likes Problem with File search using RegEx Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Problem with File search using RegEx" Watch "Problem with File search using RegEx" New topic
Author

Problem with File search using RegEx

Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
hi all,
I just wrote a program to search for files using a regex expression.



It works fine but the problem is at the end of the search it throws a NullPointerException.

Exception in thread "main" java.lang.NullPointerException
at javademo.IODemo.search(IODemo.java:30)
at javademo.IODemo.search(IODemo.java:40)
at javademo.IODemo.main(IODemo.java:22)
Java Result: 1
BUILD SUCCESSFUL (total time: 2 seconds)



I dont understand why. I know i can always catch the exception to avoid the problem
but i want to know the reason behind the problem and eliminate it in a proper way.
Thank you very much in advance.

Regards
Sudhanshu Gupta


Thanks & Regards

Sudhanshu Gupta

Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
hi again,
I just noticed one more problem with the program.

I used the regex "[a-z][a-z]+.pdf" to search for pdf files with only lowercase alphabets int their name.
I guess I am mistaken somewhere as the result includes files like

H:\Pen Drive Dump\www.free-ebooks-download.org--------C# Design Patterns A Tutorial.pdf
H:\Softwares\IT Academy Course\IT Academy\IT_ACADEMY_Instructions.pdf
H:\Softwares\New Folder\2006_4Sem_Regular.pdf
H:\Softwares\New Folder\amit\C++ persistent database component\mda_tools.pdf


Please help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Sudhanshu Gupta wrote:It works fine but the problem is at the end of the search it throws a NullPointerException.


What exactly is line 30? On that line, something on the left hand site of a ., or an array / Iterable you're iterating over in a for loop, is null. My guess is the latter; what is the value of target.listFiles()? If the folder cannot be read, it will be null.

Sudhanshu Gupta wrote:I used the regex "[a-z][a-z]+.pdf" to search for pdf files with only lowercase alphabets int their name.
I guess I am mistaken somewhere as the result includes files like

H:\Pen Drive Dump\www.free-ebooks-download.org--------C# Design Patterns A Tutorial.pdf
H:\Softwares\IT Academy Course\IT Academy\IT_ACADEMY_Instructions.pdf
H:\Softwares\New Folder\2006_4Sem_Regular.pdf
H:\Softwares\New Folder\amit\C++ persistent database component\mda_tools.pdf

Your regular expression only checks if the name contains two or more lowercase letters, followed by any character and lowercase PDF. If there are more characters before or after that part that's just fine.
To ensure that the entire string is used, enclose the regular expression in "^" (start of string) and "$" (end of string). Also, escape the . (use a double backslash - one for the regular expression, and one for Java): "^[a-z][a-z]+\\.pdf$".


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
Thank you very much Rob
The regular expression you suggested works good. I did'nt know about the start and end characters.

The line 30 corresponds to



somehow target becomes null for the last iteration/recursion.
But as you can notice i've checked it for null just before this line.



Thus line 30 is reached only if target is not null and is a directory.
I fail to understand whats wrong here.
Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
hi all
I found out the solution for my problem after debugging the code.
NullPointerException is thrown when target = H:\System Volume Information.
May be the system denies access to list files in this system folder.
That is why the exception is thrown only when we give target as a drive (C:\, D:\ etc).
while if the target is a directory (say D:\Tutorials) no exception is thrown.
That is why it throws the exception.

Now is there any way I can check for a System File/directory or do I need to check for
this particular Directory?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Windows indeed denies access to this folder, even if you're an administrator.

As I said before "If the folder cannot be read, it will be null." So you'll just need to build in a null-check so all folders you can't access are safe:
Sudhanshu Gupta
Greenhorn

Joined: May 02, 2009
Posts: 23
Thank you very much Rob.
The program runs absolutely fine now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with File search using RegEx
 
Similar Threads
regular expressions
Usage of Regular expressions
directory creation thru java program
Java regex?
Regular Expression: finding multiple lines