• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Rajesh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a hint...
Inside class A, make this change:

Now, frame c will be disposed of, if frame b has created one. Otherwise you'll still throw an exception.
Was this enough of a hint?
 
Rajesh Kumar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mr.Schreckmann,
Thank you very much.
Your reply helped me alot.
Millions of Thanks to you.
Yours
Rajesh

Originally posted by Dirk Schreckmann:
Here's a hint...
Inside class A, make this change:

Now, frame c will be disposed of, if frame b has created one. Otherwise you'll still throw an exception.
Was this enough of a hint?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic