aspose file tools*
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


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Event Handling Problem" Watch "Event Handling Problem" New topic
Author

Event Handling Problem

vijay malhotra
Greenhorn

Joined: Nov 26, 2000
Posts: 26
Following are the 3 files a1.java , b1.java and e1.java
a1.java & b1.java are the applet files while e1.java 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 e1.java
But when I Execute the program it throws the following exception
E:\javaprog\testing>appletviewer a1.java
Exception occurred during event dispatching:
java.lang.NullPointerException
at e1.actionPerformed(e1.java:20)
at java.awt.Button.processActionEvent(Button.java:329)
at java.awt.Button.processEvent(Button.java:302)
at java.awt.Component.dispatchEventImpl(Component.java:2595)
at java.awt.Component.dispatchEvent(Component.java:2499)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:319)
at java.awt.EventDispatchThread.pumpOneEvent(EventDispatchThread.java:10
3)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:84)

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

public void paint(Graphics g)
{
g.drawString(str,50,80);
}
}
/** b1.java **/
import java.awt.*;
import java.applet.*;
/*<applet code=b1 width=200 height=200>
</applet>*/
public class b1 extends Applet
{
e1 ee = new e1(this);
String msg = "HI THERE";
public void paint(Graphics g)
{
g.drawString(msg,40,50);
}
}
/** e1.java **/
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";
a.repaint();
b.msg = "Hello World";
b.repaint();

}
}

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.
Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Event Handling Problem