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 ?