permaculture playing cards
The moose likes Swing / AWT / SWT and the fly likes Event Handling Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handling Problem" Watch "Event Handling Problem" New topic

Event Handling Problem

vijay malhotra

Joined: Nov 26, 2000
Posts: 26
Following are the 3 files , and & are the applet files while is
the event handling file.
What I want the output is that when I press the button in
applet of a1 then the text in applet b1 should change
from "HI THERE" to "Hello World" , this change of
message is done in class
But when I Execute the program it throws the following exception
Exception occurred during event dispatching:
at e1.actionPerformed(
at java.awt.Button.processActionEvent(
at java.awt.Button.processEvent(
at java.awt.Component.dispatchEventImpl(
at java.awt.Component.dispatchEvent(
at java.awt.EventQueue.dispatchEvent(
at java.awt.EventDispatchThread.pumpOneEvent(
at java.awt.EventDispatchThread.pumpEvents(

Following is the code for the 3 files
/** **/
import java.awt.*;
import java.applet.*;
/*<applet code=a1 width=200 height=200>
public class a1 extends Applet
Button b1 =new Button("Change It");
e1 e = new e1(this);
String str = "";
public void init()

public void paint(Graphics g)
/** **/
import java.awt.*;
import java.applet.*;
/*<applet code=b1 width=200 height=200>
public class b1 extends Applet
e1 ee = new e1(this);
String msg = "HI THERE";
public void paint(Graphics g)
/** **/
import java.awt.event.*;
public class e1 implements ActionListener
a1 a;
b1 b;
e1(a1 a)
this.a = a;

e1(b1 b)
this.b = b;
public void actionPerformed(ActionEvent ae)

a.str = "Just Do It";
b.msg = "Hello World";


Q1. Why the exception is being thrown , please do tell the cause ?
Q2. How can I correct this problem so that I get the desierd output ?
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Vijay,
The appletviewer only handles HTML pages. You don't give your java code to appletviewer you supply a HTML page that calls your applet code (java file).
Assuming you get your HTML file setup correctly with two applet tags one for each a1 and b1, you still will have a problem because you are making an assumption about e1 that is invalid.
In each class a1 and b1 you are creating an instance of e1. Therefore you have two different e1 objects:
One has a valid a1 reference but null b1 reference
One has a valid b1 reference but null a1 reference
When you click on the button, whichever instance gets invoked will get a NullPointerException because it doesn't have two valid references. In order to get around that, you need to make your references in e1 class static. That way any instance will be looking at the same a1 or b1 references. Of course, the requirement that both applets be loaded still exists. To make your code more robust you will want to check:
if( obj != null )
// use it here
to make sure you have a good object before you try using it.
I agree. Here's the link:
subject: Event Handling Problem
It's not a secret anymore!