nullPointerException

Reply

Join Date: May 2007
Posts: 12
Reputation: nam5a is an unknown quantity at this point 
Solved Threads: 0
nam5a nam5a is offline Offline
Newbie Poster

nullPointerException

 
0
  #1
Jul 1st, 2009
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

  1. import java.io.BufferedReader;
  2. import java.io.BufferedWriter;
  3. import java.io.File;
  4. import java.io.FileWriter;
  5. import java.io.IOException;
  6. import java.io.InputStreamReader;
  7. import java.io.Writer;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class Check {
  12.  
  13. public static File searchForFile(File rootDir, String filename){
  14. File files[] = rootDir.listFiles();
  15. if (containsFile(files, filename)) {
  16. return getFile(files, filename);
  17. }
  18. else {
  19. for (int i = 0; i < files.length; i++) {
  20. if (files[i].isDirectory()) {
  21. searchForFile(files[i], filename);
  22. }
  23. }
  24. }
  25. return null;
  26. }
  27.  
  28. public static boolean containsFile (File files[], String filename) {
  29. String getName;
  30. for (int i = 0; i < files.length; i++) {
  31. getName = files[i].getName();
  32. if (getName.equals(filename))
  33. return true;
  34. }
  35. return false;
  36. }
  37.  
  38.  
  39. public static File getFile (File files[], String filename) {
  40. String pathName;
  41. for (int i = 0; i < files.length; i ++) {
  42. pathName = files[i].getName();
  43. if (pathName.equals(filename)) {
  44. System.out.println("File has been retrieved: " + files[i].getAbsolutePath());
  45. return files[i];
  46. }
  47. }
  48. return null;
  49. }
  50.  
  51. public static void main(String[] args) throws IOException {
  52.  
  53. //getting the information
  54. String [][] people = new String [100][5];
  55. String [] cells = null;
  56. String line = null;
  57. int col = 0;
  58. String fn = null;
  59. File rootDir = new File("C:\\");
  60. BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
  61. 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?: ");
  62. fn = in.readLine();
  63. if (!(fn.contains(".csv"))) {
  64. fn += ".csv";
  65. }
  66. Scanner myScanner = null;
  67. File fl = searchForFile(rootDir, fn);
  68. if (fl == null) {
  69. System.out.println("Program exiting. Error in locating file. Make sure you enter file name correctly.\nPress any key to continue.");
  70. fn = in.readLine();
  71. System.exit(0);
  72. }
  73. myScanner = new Scanner(fl);
  74. }
  75. }
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: nullPointerException

 
0
  #2
Jul 1st, 2009
The Exception tells you the method & line number where it was thrown. Perhaps you could share that info with us?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: nam5a is an unknown quantity at this point 
Solved Threads: 0
nam5a nam5a is offline Offline
Newbie Poster

Re: nullPointerException

 
0
  #3
Jul 1st, 2009
Exception in thread "main" java.lang.NullPointerException
at Check.containsFile(Check.java:70)
at Check.searchForFile(Check.java:55)
at Check.searchForFile(Check.java:61)
at Check.main(Check.java:107)
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: nullPointerException

 
0
  #4
Jul 1st, 2009
That's a good start. Which line exactly is line 70?
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: nam5a is an unknown quantity at this point 
Solved Threads: 0
nam5a nam5a is offline Offline
Newbie Poster

Re: nullPointerException

 
0
  #5
Jul 1st, 2009
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);
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: nullPointerException

 
0
  #6
Jul 1st, 2009
Null pointer in first line can only mean "files" is null. Check this by trying to print it immediately before this line. Look at the code that passes it as a parameter to see if/how it could be null
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: nam5a is an unknown quantity at this point 
Solved Threads: 0
nam5a nam5a is offline Offline
Newbie Poster

Re: nullPointerException

 
0
  #7
Jul 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: nullPointerException

 
0
  #8
Jul 1st, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 12
Reputation: nam5a is an unknown quantity at this point 
Solved Threads: 0
nam5a nam5a is offline Offline
Newbie Poster

Re: nullPointerException

 
0
  #9
Jul 1st, 2009
I think I may be a little confused, I thought I was doing that.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 964
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 143
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: nullPointerException

 
0
  #10
Jul 1st, 2009
You have this loop:
  1. for (int i = 0; i < files.length; i++) {
  2. if (files[i].isDirectory()) {
  3. searchForFile(files[i], filename);
  4. }
  5. }

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

  1. for (int i = 0; i < files.length; i++) {
  2. if (files[i].isDirectory()) {
  3. File f =searchForFile(files[i], filename);
  4. if (f != null) return f;
  5. }
  6. }
Last edited by JamesCherrill; Jul 1st, 2009 at 5:36 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC