2015-11-12 82 views
0

我正在使用try/catch作为仅接受整数的对话框的错误输入。我的电讯局长告诉我需要导入一些东西。我试过,但仍然获得第8行同样的错误:无法将NullPointerException解析为变量

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"); 
    } 
} 

任何人都可以提供一些线索?

+2

的可能的复制[什么是空指针异常,并且我怎么修复它?(http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – LokiSinclair

回答

1

如果你想检查你的对象e是一个NullPointerException,你必须使用运营商instanceof而不是==

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