在Java中声明信号量时出现NullPointerException?

时间:2015-02-15 15:08:29

标签: java exception concurrency nullpointerexception semaphore

当我尝试运行此代码时,我得到NullPointerException

private static Semaphore[] trackSemas;

for(int i=0;i<9;i++){
            trackSemas[i] = new Semaphore(1,true);
        }

为什么这段代码没有得到NullPointerException?

1 个答案:

答案 0 :(得分:3)

您需要初始化trackSemas数组:

private static Semaphore[] trackSemas = new Semaphore[9];