需要帮助,为什么这里有java.lang.NullPointerException

时间:2018-10-18 09:57:42

标签: java

为什么这里有

Exception  in thread "Thread-0" at unsynch.Bank.transfer(Bank.java:30)
    at unsynch.UnsynchBankTest.lambda$0(UnsynchBankTest.java:22)
    at java.lang.Thread.run(Unknown Source)
Thread[Thread-3,5,main]Exception in thread "Thread-2" java.lang.NullPointerException

代码:

public void transfer(int from, int to, double amount) throws InterruptedException
{
    bankLock.lock();
    try
    {
        if(accounts[from] < amount) 
            sufficientFunds.await();    //当前条件不符合。被阻塞,放弃锁
        System.out.print(Thread.currentThread());
        accounts[from] -= amount;
        System.out.printf("%10.2f from %d to %d", amount, from, to);
        accounts[to] += amount;
        System.out.printf(" Total Balance: %10.2f%n",getTotalBalance());
        sufficientFunds.signal();  //通知阻塞线程激活。
    }
    finally
    {
        bankLock.unlock();
    }
}

0 个答案:

没有答案