Solved

Javis: a small this.getParent() problem. JB 5........

Posted on 2002-07-08
8
478 Views
Last Modified: 2008-02-26
Hiyah!

I have a problem with this snippet of code in JBuilder 5, from the program at (http://cs.baylor.edu/~donahoo/NIUNet/javis.html), its in the Javis.Java file...:

Container parent = this;
               
          while ( !( parent instanceof Frame ) )
               parent = parent.getParent();

          // Create the interface object to draw widgets and handle events.
          try {
               m_iface=new SwingInterface( (Frame) parent, getSize(), getCodeBase(), filename );
               // Add the interface to the applet.
               getContentPane().add( m_iface );
               m_run_ok = true;
          }
          catch (Exception e) {
               m_run_ok = false;
               System.err.println( e.getMessage() );
                        System.err.println("Error in Init!!!");
          }

The 'parent = parent.getParent();' bit keeps quitting with a null pointer exception.

What's going on? This code apparently works in other development environments, so why not JB 5?
0
Comment
Question by:Lab_Rat
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 7138041
Well, in the contexts you've seen it working before, you were probably looking at Frame-based stuff. In this  case, you're using an applet, which extends Panel. It *is* a container, hence the assignment:

Container parent = this;

works.

This also works:

while ( !( parent instanceof Frame ) )

as an applet is not an instanceof Frame.

Therefore

parent = parent.getParent();

works. But since the applet does not *have* a parent, you get a null pointer.


0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7138229
The highest level parent is not a frame. The loop executes until parent is referencing the highest level container. Since this is not a frame, the loop continues once more. But since parent does not have a parent the method getParent() returns null. The loop then continues, but the line:
(parent instanceof Frame)
throws an exception since null cannot be used with the instaceof operator.
0
 
LVL 4

Accepted Solution

by:
kylar earned 50 total points
ID: 7138493
Change your code from this:

while ( !( parent instanceof Frame ) )
              parent = parent.getParent();

to:

while ( ! (( parent instanceof Window) && ( parent instanceof Applet)) )
              parent = parent.getParent();

And that will get you the proper top level window. Alternately you could use SwingUtilities.getWindowAncestor() call. You will also have to modify the constructor:

m_iface=new SwingInterface( (Frame) parent, getSize(), getCodeBase(), filename );

To not pass a frame, but instead to pass whatever is proper (perhaps a container).

Cheers,
Kylar
0
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 
LVL 92

Expert Comment

by:objects
ID: 7139122
> But since the applet does not *have* a parent

Applets can have parents, and generally do.

I guess JBuilder mustn't run the applet in a browser, and instead runs it somehow in it's own container.

The problem is that your loops searches the component hierarchy for an instance of a Frame. If it doesn't find one then when it gets to the top of the tree you'll get an npe. So just change your code to test for null:

while ( parent!=null &&  !(parent instanceof Frame) )
              parent = parent.getParent();

or use:

Frame frame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, this);
0
 
LVL 4

Expert Comment

by:kylar
ID: 7139151
D'Oh.. my code change should be an OR instead of an AND.. change the && to an ||

Cheers
0
 
LVL 1

Author Comment

by:Lab_Rat
ID: 7140118
Thanks to everyone, but especially Kylar and objects!

Not only did you give two answers, one in the original algorithem style, and a new one utilising the API more efficiently, but both examples worked first time, and solved my problem.

Points for Kylar in this answer, and I've posted some points for you too objects on this board.
0
 
LVL 3

Expert Comment

by:yasser_helmy
ID: 7140320
Hi all,
i think there is a problem with 'objects' code. the exception will not be thrown but the parent var will still be null. to fix this, use
Container parent = this;
Container son = this;
  while (parent!=null && !(parent instanceof Frame)){
    son = parent;
    parent = parent.getParent();
  }
  //when you get here, parent will either reference a frame or become null
  if(parent ==null)
    parent = son;

0
 
LVL 92

Expert Comment

by:objects
ID: 7140360
> i think there is a problem with 'objects' code

Watch what you're saying :-)

> the exception will not be thrown but the parent var
> will still be null

That's true if an instance of Frame is not found in the component hierarchy. And is desirable, as I am assuming that the SwingInterface ctor expects a Frame instance.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 53
JUnit 4 @Before and @BeforeClass differences 3 60
diffSum example 4 37
CSV file parsing thru Java 13 31
For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question