Java JOptionPane提供了一个NullPointerException,不显示
下面是一段代码片段Java JOptionPane提供了一个NullPointerException,不显示,java,swing,nullpointerexception,try-catch,joptionpane,Java,Swing,Nullpointerexception,Try Catch,Joptionpane,下面是一段代码片段 private void viewscriptJButtonActionPerformed(ActionEvent evt){ try{ for (int j = 0; j <= i; j++) { scriptPane.setText(queryList.get(j).toString() + "\n");
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);
}
}
private void viewscriptJButtonActionPerformed(ActionEvent evt){
试一试{
对于(int j=0;j,当异常的原因是自身时,您会发现返回null
,因此e.getCause().toString()
将抛出NullPointerException。getCause方法通常用于在将它们链接在一起时查找根异常
将打印出异常的整个堆栈跟踪,以便更轻松地进行调试,但是要修复此错误,只需将JOptionPane语句更改为:
JOptionPane.showMessageDialog(this,"error:\n"+e.getMessage().toString(),"Error",JOptionPane.ERROR_MESSAGE);
另外,您可能想看看。我猜try
行上缺少的open{
只是一个复制粘贴错误?添加e.printStackTrace()在catch块中,您将得到一个堆栈跟踪,精确指示NullPointerException发生的位置。为了更快地获得更好的帮助,请发布@chrsblck-yes,对不起。这只是一个复制粘贴错误。