简体   繁体   中英

Inner class not able to access outer class member variable which is initialized from a method which internally calls constructor

Below code throwing nullpointer Exception due to uninitialized outer member variable. it is throwing NPE in run method for runnable variable. I have initialized local valiable of type ConcurrentLinkedQueue in newFixedThreadPool method and called parameterized constructor to initialized the runnable member variable.

BTW when I directly initialize the runnable variable without creating any local variable code runs successfully.

Can Anyone explain this

import java.util.concurrent.ConcurrentLinkedQueue;

public class MyExecuterService  {

    private  ConcurrentLinkedQueue<Runnable> runnables;
    //private AtomicBoolean execute=AtomicBoolean.true;
    private PoolWorkerThread poolWorkerThreads[];

    public MyExecuterService()
    {}

    public MyExecuterService(ConcurrentLinkedQueue<Runnable> runnables,PoolWorkerThread poolWorkerThreads1[])
    {
        this.runnables=runnables;
        poolWorkerThreads=poolWorkerThreads1;
    }

    private class PoolWorkerThread extends Thread{

        @Override
        public void run() {
            while(true) {
                System.out.println(runnables.size()+"runnable"+runnables);
                synchronized (runnables) {

                    if(runnables.size()==0)
                    {
                        try {
                        runnables.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                    else
                    {
                        Runnable task=runnables.poll();
                        task.run();
                        //notify();
                    }
                }   
            }
        }
    }

    public  MyExecuterService newFixedThreadPool(int i)
    {
        ConcurrentLinkedQueue<Runnable> runnableQueue= new ConcurrentLinkedQueue<Runnable>();

        PoolWorkerThread [] threads= new PoolWorkerThread[i];
        for (int j = 0; j < i; j++) {
            threads[j]= new PoolWorkerThread();
            threads[j].start();
        }
        return new MyExecuterService(runnableQueue, threads);
    }

    public void execute(Runnable runnable) {
        System.out.println(runnables.size());
        synchronized(runnables)
        {
            runnables.add(runnable);
            runnables.notify();
        }
    }
    public ConcurrentLinkedQueue<Runnable> getRunnables() {
        return runnables;
    }

    public void setRunnables(ConcurrentLinkedQueue<Runnable> runnables) {
        this.runnables = runnables;        
    }
}

The NullPointerException is due to the data members of MyExecuterService not being initialized.

You created the MyExecuterService object with parameters in the new FixedThreadPool(int i) method, but since this method belongs to MyExecuterService itself, so to access this method you must have created a MyExecuterService object. Then only you could access the object.

Now the exception is occurring due to creating MyExecuterService object with the empty parameters constructor so the data members are not being initialized.

Just check through which object you are calling the newFixedThreadPool(int i) method and ensure that that object is being created with the parameterized constructor.

By the way, your design of MyExecuterService class is kind of hypothetical.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM