Following is my code to test some grammar. I want return a object in try-block and do something in finally-block. But I get NullPointException when I run my code.The cause is using ReentrantLock
, but I don't know why.
import java.util.concurrent.locks.ReentrantLock;
public class Main {
ReentrantLock lock;
public static void main(String[] args) {
try {
new Main().run();
} catch (Exception e) {
e.printStackTrace();
}
}
public void run() {
System.out.println(returnTest().get());
}
A returnTest(){
// lock.lock();
try {
return new A();
} finally {
// lock.unlock();
}
}
}
class A {
public boolean get() {
return true;
}
}
If I uncomment lock.lock()
and lock.unlock()
, I will get NullPointException.
Why?