1

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]);                    
            }
        }
    }
}
py_script
  • 808
  • 2
  • 16
  • 38
  • When you are asking questions about an exception, always provide the actually exception, the first couple of lines, and point out _which_ line corresponds to the exception line number. Please edit your post to provide this information. – Gray Sep 18 '12 at 17:33
  • 1
    Can you format your code properly? It's really hard to follow when your indentation is all over the map. – Mark Peters Sep 18 '12 at 17:40
  • 1
    Looks like problem in the SystemDir constructor – bhatanant2 Sep 18 '12 at 17:43
  • 2
    Are you really starting the thread in a loop? You can only do that once right? Do you mean to start a new thread on each element in the array? – Gray Sep 18 '12 at 17:57
  • 1
    Also, I'm still unsure what line is throwing the NPE. The `t.start()` line? That doesn't make sense tho. – Gray Sep 18 '12 at 17:58
  • What's the problem with starting a thread in a loop? – py_script Sep 20 '12 at 10:24

5 Answers5

3

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 null
  • none of the subFiles[i] is null
  • lemma is not null

(add println statements if necessary)

and as pointed out by @Gray, you can't start a thread more than once.

Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
2

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.

hmakholm left over Monica
  • 23,074
  • 3
  • 51
  • 73
  • Yes, you have a point on the "list() returns null thing". I will post the SystemDir source code, in the afternoon. :) – py_script Sep 20 '12 at 10:26
0

You have not included all the code.

With the information provided:

in searchFiles either t, subFiles, or subFiles[i] is null.

Skip Head
  • 7,580
  • 1
  • 30
  • 34
0

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.

Has QUIT--Anony-Mousse
  • 76,138
  • 12
  • 138
  • 194
0

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.

midhunhk
  • 5,560
  • 7
  • 52
  • 83