| | |
nullPointerException
![]() |
•
•
Join Date: May 2007
Posts: 12
Reputation:
Solved Threads: 0
I've created my own recursive file search. When the file is found it is printed that it is found, but the method searchForFile() does not terminate once the file is found, it continues to iterate and I believe this is when the nullPointerException is invoked. I've looked over this and cannot figure out what the problem is. Any help would be appreciated. Thanks
Java Syntax (Toggle Plain Text)
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.Writer; import java.util.Scanner; public class Check { public static File searchForFile(File rootDir, String filename){ File files[] = rootDir.listFiles(); if (containsFile(files, filename)) { return getFile(files, filename); } else { for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { searchForFile(files[i], filename); } } } return null; } public static boolean containsFile (File files[], String filename) { String getName; for (int i = 0; i < files.length; i++) { getName = files[i].getName(); if (getName.equals(filename)) return true; } return false; } public static File getFile (File files[], String filename) { String pathName; for (int i = 0; i < files.length; i ++) { pathName = files[i].getName(); if (pathName.equals(filename)) { System.out.println("File has been retrieved: " + files[i].getAbsolutePath()); return files[i]; } } return null; } public static void main(String[] args) throws IOException { //getting the information String [][] people = new String [100][5]; String [] cells = null; String line = null; int col = 0; String fn = null; File rootDir = new File("C:\\"); BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Save the senority list excel spreadshet as a .csv file. You do this buy opening the file and going to save as.\nMake sure you save the file in a location you can remember.\nWhat is the name of your file?: "); fn = in.readLine(); if (!(fn.contains(".csv"))) { fn += ".csv"; } Scanner myScanner = null; File fl = searchForFile(rootDir, fn); if (fl == null) { System.out.println("Program exiting. Error in locating file. Make sure you enter file name correctly.\nPress any key to continue."); fn = in.readLine(); System.exit(0); } myScanner = new Scanner(fl); } }
•
•
Join Date: May 2007
Posts: 12
Reputation:
Solved Threads: 0
I replaced the old line numbers with the actual line of code
Exception in thread "main" java.lang.NullPointerException
at Check.containsFile --> for (int i = 0; i < files.length; i++) {
at Check.searchForFile --> if (containsFile(files, filename)) {
at Check.searchForFile --> searchForFile(files[i], filename);
at Check.main --> File fl = searchForFile(rootDir, fn);
Exception in thread "main" java.lang.NullPointerException
at Check.containsFile --> for (int i = 0; i < files.length; i++) {
at Check.searchForFile --> if (containsFile(files, filename)) {
at Check.searchForFile --> searchForFile(files[i], filename);
at Check.main --> File fl = searchForFile(rootDir, fn);
•
•
Join Date: May 2007
Posts: 12
Reputation:
Solved Threads: 0
When the method searchForFile() is ran, it prints out that the file is found (if you created it), once this happens the method should terminate but it does not. I don't understand why the method searchForFile() does not terminate. It continues iterating and this is when the nullPointerException is returned.
•
•
Join Date: Apr 2008
Posts: 971
Reputation:
Solved Threads: 146
Quick guess:
The search method is recursive. When you find the file the current invocation terminates, but the invocation that called it is not terminated. I think what you need is that when you find the file, no matter how deep in the recursion stack, the whole stack is terminated.
When you make the recursive call you ignore the return value; maybe you could check the returned value and, if it is the file, immediately return that.
The search method is recursive. When you find the file the current invocation terminates, but the invocation that called it is not terminated. I think what you need is that when you find the file, no matter how deep in the recursion stack, the whole stack is terminated.
When you make the recursive call you ignore the return value; maybe you could check the returned value and, if it is the file, immediately return that.
•
•
Join Date: Apr 2008
Posts: 971
Reputation:
Solved Threads: 146
You have this loop:
This will continue to loop regardless of what the searchForFile sub-call returns. You then exit the loop and return null
You need to exit this loop as soon as searchForFile returns a file, and you need to do that by returning that file, eg
Java Syntax (Toggle Plain Text)
for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { searchForFile(files[i], filename); } }
This will continue to loop regardless of what the searchForFile sub-call returns. You then exit the loop and return null
You need to exit this loop as soon as searchForFile returns a file, and you need to do that by returning that file, eg
Java Syntax (Toggle Plain Text)
for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { File f =searchForFile(files[i], filename); if (f != null) return f; } }
Last edited by JamesCherrill; Jul 1st, 2009 at 5:36 pm.
![]() |
Similar Threads
- nullpointerexception error (Java)
- nullpointerexception (Java)
- error :java.lang.NullPointerException in searchuser.jsp page (JSP)
- NullPointerexception ?? (Java)
- Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException (Java)
- [B]NullPointerException[/B] (Java)
- Multi class multi form j2me app getting NullPointerException (Java)
- NullPointerException: (Java)
Other Threads in the Java Forum
- Previous Thread: file transfere on j2me
- Next Thread: url case sensitive
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation bi binary blackberry block bluetooth character class client code compile compiler component consumer database desktop developmenthelp eclipse error fractal freeze ftp functiontesting game gameprogramming givemetehcodez graphics gui health html ide image integer j2me j2seprojects java javac javaee javaprojects jetbrains jni jpanel jtable julia learningresources lego linked linux list mac main map method methods mobile myregfun netbeans notdisplaying number online printf problem program project qt recursion researchinmotion rotatetext rsa scanner screen server set singleton sms sort spamblocker sql string swing system textfields threads time title tree tutorial-sample update variablebinding windows working xor





