简体   繁体   中英

NullPointerException cannot be resolved to a variable

I am using try/catch for an incorrect input for a dialog box that only accepts integers. I was told by my TA that I need to import something. I tried this but still get the same error on line 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");
    }
}

Can anyone shed some light?

If you want to check if your object e is a NullPointerException, you have to use the operator instanceof instead of == :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM