NullPointerException 无法解析为变量

NullPointerException cannot be resolved to a variable

我正在使用 try/catch 为只接受整数的对话框输入不正确的内容。我的助教告诉我我需要导入一些东西。我试过了,但在第 8 行仍然出现相同的错误:

import java.lang.NullPointerException; //put this where it should be


try
{
    buttons.rotatebutton(); //method I created
}
catch (NumberFormatException | NullPointerException e)
{
    System.out.println("Please type a number");
    if (e == NullPointerException) //ERROR OCCURS HERE
    {
        System.out.println("User cancelled");
    }
}

任何人都可以解释一下吗?

如果你想检查你的对象 e 是否是 NullPointerException,你必须使用运算符 instanceof 而不是 == :

if (e instanceof NullPointerException) {
    System.out.println("User cancelled");
}