I recently began working with Threads and I am trying to complete a Java implementation of the Looper class in Android. Basically I am making a Java class that puts threads into a queue that will then be executed by the Looper class. I have the code completed for the most part but have an issue with the enqueuing of tasks.
In the Looper class I have the queue declared and my enqueue method:
List<Runnable> queue;
public synchronized void enqueue(Runnable runnable) {
queue.add(runnable);
notify(); // signal a waiting thread
}
I then created another class called TaskManager to add Tasks into the queue. I receive the error when I call:
loop.enqueue(new Task());
Where Task() implements runnable and just adds two integers together in its run() method...this is just a test.
The error I receive is:
Exception in thread "Thread-0" java.lang.NullPointerException
at Looper.enqueue(Looper.java:20) (this is the queue.add(runnable))
at TaskMaker.run(TaskMaker.java:16) (this is the loop.enqueue(new Task())开发者_StackOverflow社区
I'm obviously doing something wrong and not implementing this right...how should I go about this? Is the way I am enqueuing the task right? Thanks for any help it is much appreciated!
Are you initializing the queue variable? like:
List<Runnable> queue = new ArrayList<Runnable>();
精彩评论