Reputation: 830
EDIT:
After making all the changes you suggested, the problem remained. The debugger said the lemma variable was null, but the fixes I applied didn't make things better. So, due to deadline issues, I decided to approach the problem from another view. Thank you all for your help. :)
I am writing a small program and a NullPointerException
drives me crazy. I have two classes: SystemDir
and Search
. The first one is just an encapsulation of initial directory and a search lemma. The Search
class is shown below. Briefly, I want one thread to search the first level directory and the other one to expand the subdirectories. That's where I get the exception. The exception string is
Exception in thread "Thread-0" java.lang.NullPointerException
at Search.searchFiles(Search.java:59)
at Search.<init>(Search.java:53)
at SystemDir.<init>(SystemDir.java:61)
at Search$1.run(Search.java:45)
at java.lang.Thread.run(Thread.java:679)
Where the 3 points are t.start() inside the final loop, searchFiles method call, some lines above and the new SystemDir call in the run method. Can you help me please?
public class Search {
private Thread t;
public Search(String[] subFiles, final String[] subDir, final String lemma) {
t = new Thread(new Runnable() {
@Override
public void run() {
for(int i=0;i<subDir.length;i++) {
try {
System.out.println(subDir[i]);
new SystemDir(subDir[i], lemma);
}
catch (NoDirectoryException ex) {
Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
searchFiles(subFiles,lemma);
}
private void searchFiles(String[] subFiles, String lemma) {
for(int i=0;i<subFiles.length;i++) {
t.start();
if(subFiles[i].contains(lemma)) {
System.out.println(subFiles[i]);
}
}
}
}
Upvotes: 1
Views: 403
Reputation: 23332
You have failed to posted the source code for SystemDir
, but the stack trace says that its constructor is trying to create a new Search
object in addition to the one that created the thread in the first place.
More concretely, probably the new Search(...)
expression somewhere in SystemDir's constructor is passing null
for subFiles
. Is there a call to File.list()
somewhere that you haven't checked for a null return from, perhaps? Note that list()
returns null if it cannot list a directory at all, due to anything from missing permissions to directory-not-found.
Also, It appears you're attempting to start the same thread object more than once. That will cause an IllegalThreadStateException
if there is ever more than one element in subFiles
.
Upvotes: 2
Reputation: 5554
In your searchFiles
method, what is the point of starting the thread in a loop? Do you want to run the thread on each execution of the loop? I think you are missing something here.
Check if some value that you are passing to the constructor is null.
Upvotes: 0
Reputation: 77454
Your code itself doesn't make much sense.
That makes it hard to spot the error.
I recommend using the Eclipse debugger, and check WHICH value is null
.
As far as I can tell, your problem is within the recursion into SystemDir
, where you don't provide the code of.
Upvotes: 0
Reputation: 328568
As a rule, never start a thread from a constructor. It can create all sorts of issues, which may be responsible for the exception you get.
Create the thread like you do in your constructor, make searchFiles
public and call that method from the client code, not from the constructor.
Apart from that, have you checked that:
subFiles
is not nullsubFiles[i]
is nulllemma
is not null(add println statements if necessary)
and as pointed out by @Gray, you can't start a thread more than once.
Upvotes: 3
Reputation: 7760
You have not included all the code.
With the information provided:
in searchFiles
either t
, subFiles
, or subFiles[i]
is null.
Upvotes: 0