这是一个代码片段
private void viewscriptJButtonActionPerformed(ActionEvent evt){
try{
for (int j = 0; j <= i; j++) {
scriptPane.setText(queryList.get(j).toString() + "\n");
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,"error:\n"+e.getMessage().toString() + "\n" + e.getCause().toString() ,"Error",JOptionPane.ERROR_MESSAGE);
}
}
catch block 内的代码总是给出一个 NullPointerException
。这只是一个引用。每当需要捕获异常时,每个按钮单击事件都会出现问题。
任何帮助。
最佳答案
你会发现Exception#getCause()当异常原因是其本身时,返回 null,因此您的 e.getCause().toString() 将抛出 NullPointerException。 getCause 方法通常用于在将它们链接在一起时查找根异常。
printStackTrace()将打印出异常的整个堆栈跟踪,以便更轻松地进行调试,但是要修复此错误,只需将 JOptionPane 语句更改为:
JOptionPane.showMessageDialog(this,"error:\n"+e.getMessage().toString(),"Error",JOptionPane.ERROR_MESSAGE);
此外,您可能想查看this 。
关于java - JOptionPane 给出 NullPointerException 并且不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16288386/