java - How do I bypass inaccessible files? -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i trying find of files named, in directory have chosen. code have works when c:\program files, or c:\users. when c:/ stuck in recycle bin , java.lang.nullpointerexception , stops @ c:\$recycle.bin\s-1-5-21-1478355014-127360780-1969717230-1002144.
public void directoryserch(string target, string dirname){ file f = new file(dirname); system.out.println("h"); if(!f.isdirectory()){ throw new illegalargumentexception("that not valid directory"); } for(file folderitem : f.listfiles()){ if(folderitem.isdirectory()){ system.out.println(folderitem.getabsolutepath()); if(!folderitem.equals("")){ directoryserch(target,folderitem.getpath()); } // return result if not empty /* if (!result.equals(folderitem.getname())){ files[filesfounfd] = folderitem.getabsolutepath(); filesfounfd++; }*/ }else{ if(folderitem.getname().equals(target)){ files[filesfounfd] = folderitem.getabsolutepath(); system.out.println(folderitem.getabsolutepath()); filesfounfd++; } } } } what can not issue works in cases when not have deal recycle bin?
for(file folderitem : f.listfiles()){ the problem here. listfiles() can return null, , syntax can throw npe. change to:
file[] files = f.listfiles(); if (files != null) { for(file folderitem : files){
Comments
Post a Comment