This discussion is archived
4 Replies Latest reply: Dec 8, 2010 1:16 PM by 819621 RSS

Error creating an instance of a runnable thread

819621 Newbie
Currently Being Moderated
I'm trying to create two instances of a runnable thread but get this error message (in Eclipse):
"No enclosing instance of type GE4000 is accessible. Must qualify the allocation with an enclosing instance of type GE4000 (e.g. x.new A() where x is an instance of GE4000)."

I don't understand this error at all.

GE4000 is GE4000.java

The error occurs here and is within a protected static boolean method.

for(int t = 0; t < (int) cameraCount; ++t) {
               streamThreads[t] = new Thread(new CaptureStream(cameraHandle[t], frameBuffers[t], frameCount, basePath));
}

CaptureStream Class is defined:
     public class CaptureStream implements Runnable {
the constructor is:
     CaptureStream(Pv.tHandle handle, Pv.tFrame[] cameraFrames, int frameCount, String basePath) {

Ironically, within CaptureScreen's run method I do an analogous creation of another thread without seeing an error.

Edited: I un-nested it and it works! Thanks

Edited by: Shellback3 on Dec 8, 2010 1:13 PM
  • 1. Re: Error creating an instance of a runnable thread
    Kayaman Guru
    Currently Being Moderated
    I'd recommend avoiding using inner classes or at least learning how to use them before actually using them.

    Or do you really need to have CaptureStream as an inner class?
    In any case you're trying to create one in a static method, where no enclosing instance of the outer class (GE4000) is available. So at the very least you'd need to make it a static nested class.
  • 2. Re: Error creating an instance of a runnable thread
    819621 Newbie
    Currently Being Moderated
    Kayaman wrote:
    I'd recommend avoiding using inner classes or at least learning how to use them before actually using them.
    I'm new at Java and that's why I posted to the forum.
    Or do you really need to have CaptureStream as an inner class?
    In any case you're trying to create one in a static method, where no enclosing instance of the outer class (GE4000) is available. So at the very least you'd need to make it a static nested class.
    OK, this is my structure:

    public class GE4000 {
    ...
    class CaptureStream implements Runnable {
    ...
    run() {}
    }

    boolean startStreamCapture() {}

         public static void main(String[] args) {

    startStreamCapture();
    }
    }

    The method startStreamCapture was static and when I made it to non-static the error went away.
    However I can't use the method in main because it's not static.

    Do I understand correctly that the way to solve this is to put class CaptureStream into CaptureStream.java? That is, un-encapsulate it and then make startStream static?

    BTW My original version extended Thread and it compiled (but didn't do what I wanted) the error cropped up when I changed it to implement runnable.

    I appreciate your help.
    Nate
  • 3. Re: Error creating an instance of a runnable thread
    Kayaman Guru
    Currently Being Moderated
    Shellback3 wrote:
    Do I understand correctly that the way to solve this is to put class CaptureStream into CaptureStream.java? That is, un-encapsulate it and then make startStream static?
    That's one valid way to do it, yes. Although whether startStream is static or not shouldn't matter in this case.
  • 4. Re: Error creating an instance of a runnable thread
    819621 Newbie
    Currently Being Moderated
    Thanks, now that I'm past that I can attend to other problems!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points