我想在接受之前检查一个头元素但是加注
java.lang.NullPointerException
偷看时,
使用java阻塞队列
BlockingQueue sharedQueue = new LinkedBlockingQueue()
这是我的代码,有什么想法吗?
while(true){
try {
if(!sharedQueue.isEmpty()){
char ch = (char)sharedQueue.peek();
if(Character.isDigit(ch)){
digitTextField.setText(digitTextField.getText()+sharedQueue.take());
}
}
} catch (InterruptedException ex) {
Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
}
}
最佳答案
这是因为您正在转换为 char
不允许空值。另外,不要做 sharedQueue.isEmpty()
其次是 peek
- 这就是众所周知的“先检查后行动”,这是众所周知的种族原因。
你应该定义sharedQueue
作为BlockingQueue<Character>
然后使用
if ((Character c = sharedQueue.poll()) != null)
关于java - 取前如何查看BlockingQueue头元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147995/