![]() |
| ||
| nullPointerException 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 import java.io.BufferedReader; |
| ||
| Re: nullPointerException The Exception tells you the method & line number where it was thrown. Perhaps you could share that info with us? |
| ||
| Re: nullPointerException 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) |
| ||
| Re: nullPointerException That's a good start. Which line exactly is line 70? |
| ||
| Re: nullPointerException 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); |
| ||
| Re: nullPointerException 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 |
| ||
| Re: nullPointerException 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. |
| ||
| Re: nullPointerException 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. |
| ||
| Re: nullPointerException I think I may be a little confused, I thought I was doing that. |
| ||
| Re: nullPointerException You have this loop: for (int i = 0; i < files.length; i++) {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 for (int i = 0; i < files.length; i++) { |
| All times are GMT -4. The time now is 1:33 am. |
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC