Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Exception

Status
Not open for further replies.

sujitopics

Programmer
Oct 9, 2001
69
KR
Dear Friends,
In my application, I have 3 Frames. like A.java, B.java, and C.java
I have to close C.java Frame using a button click from A.java.
When i am trying to close C Frame from A Frame,
it is saying java.nullpointer exception.
for eg : I have 3 Frame classes like A, B and C.
B Frame will be opened using a button click from A Frame.
C Frame will be opened using a button click from B Frame.
I have to close C Frame using a Button click from A Frame.

I am new to java. Please help me.
Thanks in advance.
Looking forward to hearing from
My A.java, B.java and C.java programs are like this.
All the Frames are opening fine. only problem is while closing C Frame from A Frame
=======================
A.Java
=======================
import java.awt.*;
import java.awt.event.*;

public class A extends Frame implements ActionListener
{
B b;
C c;
Button showBChild, closeCChild;
A()
{
setLayout(new FlowLayout());
showBChild = new Button("Show B Frame");
add(showBChild);
showBChild.addActionListener(this);

closeCChild = new Button("Close C Frame");
add(closeCChild);
closeCChild.addActionListener(this);

setVisible(true);
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == showBChild) {
b = new B();
}
if(ae.getSource() == closeCChild) {
c.dispose();
}
}
public static void main(String args[]) {
new A();
}
}
=======================
B.Java
=======================

import java.awt.*;
import java.awt.event.*;

public class B extends Frame implements ActionListener
{
C c;
Button showChild;
B()
{
setLayout(new FlowLayout());
showChild = new Button("Show C Frame");
add(showChild);
showChild.addActionListener(this);

setVisible(true);
setSize(300,300);
show();
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == showChild) {
c = new C();
}
}
public static void main(String args[])
{
new B();
}
}
=======================
C.Java
=======================
import java.awt.*;
import java.awt.event.*;

public class C extends Frame
{
C()
{
setLayout(new FlowLayout());
setVisible(true);
setSize(300,300);
show();
}
}
=========================

Looking forward to hearing from you again.
 
Dear Friends,
I got the solution to my problem (to the above problem).
Thanks to all programmers for helping me.
I am posting the solution here.

Inside class A, make this change:

code:

public void actionPerformed(ActionEvent ae)
{
if (ae.getSource() == showBChild)
{
b = new B();
}
if (ae.getSource() == closeCChild)
{
b.c.dispose();
}
}
Now, frame c will be disposed of, if frame b has created one. Otherwise you'll still throw an exception.

The above code is working fine.
Cheers.


if you have any ideas or any programming techniques, please share ur ides.
Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top