0

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    }

2 Answers2

0

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.

Andreas Vogl
  • 1,776
  • 1
  • 14
  • 18
  • I don't create an Event object in the program, so no such thing is passed to the `handleEvent()` method. I suppose this method and its arguments to work in the same way as the methods of **MouseEventListener** or **KeyEventListener** interfaces and their arguments. The code I've typed coincides with the pattern of the code in the book I learned it from. I really don't have any idea about the Event object being null. –  Mar 02 '16 at 10:22
  • Please post the entire code of this class. I can't make much sense of the provided snippets. In AWT, button events are usually handle with ActionListener and ActionEvent. The method signature is called public void actionPerformed(ActionEvent e). What kind of "Event" object are you using there? – Andreas Vogl Mar 02 '16 at 10:59
  • Is it possible that you are using AWT 1.0 API? That stuff is so old, that it's hard to even find documentation about it. Where did you get a JDK that compiles this stuff? What java version are you working with? – Andreas Vogl Mar 02 '16 at 11:05
0

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.

  • 1
    Hi Progyadeep Moulik, I'm glad you could solve the problem that way. I just want to politely say that catching exceptions without logging them is a bad practice (considered an "anti pattern") as it will conceal problems. Avoiding a nullpointer with a simple "if (x != null) {...}" is much better than catching. See http://stackoverflow.com/questions/15146339/catching-nullpointerexception-in-java – Andreas Vogl Apr 03 '16 at 12:46
  • @AndreasVogl Yes sir, I've been told before also that it's bad practice. I just found this method to solve the problem at that moment. Thank you for for telling me the other way that uses an if statement. I'll use it now on. :) –  Apr 03 '16 at 13:05