当在JOptionPane上按下“取消”并且输入字符串为null时,以下代码将引发NullPointerException:
JOptionPane renamePane = new JOptionPane();
String s = renamePane.showInputDialog(null, "New layer name: ");
->在JOptionPane上按“取消”,并且输入为null时,以下代码未捕获NullPointerException:
JOptionPane renamePane = new JOptionPane();
String s = "anything at all";
try { s = renamePane.showInputDialog(null, "New layer name: "); }
catch (NullPointerException npe)
{
System.out.println("NP caught, returning");
return;
}
->不会执行print语句,但是s设置为null。为什么?
答案 0 :(得分:0)
在JOptionPane.showInputDialog(Component, Object)
Javadoc中没有任何地方说当用户取消而不输入文本时抛出NullPointerException
。
返回值为null
和抛出NullPointerException
是两回事。