简体   繁体   中英

Why does handleEvent() method throw a runtime exception?

I made a GUI file I/O application program using java.awt.Frame . There are two buttons labelled " ENTER " and " DONE ". The enter button makes the program store the data from the Textfield to the file while the done button causes the program to exit. The event of clicking the "ENTER" button is handled by the action() method while that of the "DONE" button is handled by the handleEvent() method. The program runs and does the job perfectly but the problem is whenever I click on a button, the terminal window appears behind the GUI Frame that displays a long runtime exception message. I identified one of the lines among the several in the entire exception message as pointing on a line in the handleEvent() method ( line:78 ).

See the full exception message here. (Google Docs)

Below is the definition of both the handleEvent() and action() methods. Please predict the possible causes of the runtime exception and help me solve it. Thanks.

64    public boolean action(Event event, Object o){
65        if(event.target instanceof Button){
66            if(event.arg.equals("ENTER")){
67                try{
68                    addRecord(); //calls the function to write data to the file
69                }
70                catch(Exception e){}
71            }            
72        }
73        return super.action(event,o);
74    }
...
...
76    public boolean handleEvent(Event e){
77        if(e.target instanceof Button){
78            if(e.arg.equals("DONE"))
79            System.exit(0); //exits the program
80        }
81        return super.handleEvent(e);
82    }
...
...
84    public static void main(String args[]){
85        new ClassName().prepareGUI(); //prepareGUI() setups the Frame
86    }

According to the stacktrace, on line 78 either 'e' or 'e.arg' is null.

Please provide the code where you are creating/setting the Event object that is passed into handleEvent() method.

You can easily identify the cause of a problem like this using a debugger and stepping through your code, looking at the state of your variables.

The NullPointerException was thrown in the handleEvent() method. Since the exception doesn't affect the program otherwise, I solved the problem of getting that message by using a try-catch block to catch and swallow the exception as in the code below:

public boolean handleEvent(Event e){
    try{
        if(e.target instanceof Button){
            if(e.arg.equals("DONE"))
                System.exit(0); //exits the program
        }
    }
    catch(NullPointerException npe){} //catching and swallowing
                                     //the NullPointerException
    return super.handleEvent(e);
}

Hence, I didn't have the terminal window open behind the Frame with that error message. Thanks for all the help though @AndreasVogl.

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