Solved

getCodeBase( ) and getDocumentBase( ) question

Posted on 1998-04-12
2
472 Views
Last Modified: 2010-05-18
Hi !!

I have the following problem. I have two classes in my Applet. One (the  main) called Menu and the other called Opcion. I've created in Menu class objects of Opcion class. I've used getCodeBase( ) in Menu class and it worked.. that is, it gets the URL of the class. I've used later getCodeBase( ) in Opcion class but it didn't work. Debugging the disassembled code, I realized that when getCodeBase( ) is called (from Opcion class), the method launches an exception. I've tried to pass the URL of the code as a parameter to the Opcion class, in the constructor, but it didn'0t work too. How can I do? and why getcodeBase( ) works if I call it from Menu class (the main) and it doesn't if I call it from Opcion class (whose object is created in Menu class)?

Thanks in advance
Jaime

P.S. sorry, I don't have points enough to assign to this question
0
Comment
Question by:DaThiN
2 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 10 total points
Comment Utility
Without looking at your code I can only tell you the probable cause.

Your main class is derived from Applet but your Opcion class most probably is not. Both getCodeBase() and getDocumentBase() are members of the Applet class.

To fix it, you can either send the URL returned from these calls as parameter, you can attemptinstantiate a main instance inside opcion or you can create a third class with a static URL thtas instantiated inside both main and opcion (thus acting as a global variable) with a static URL member, then main can assign it a value and opcion can read it.

Of the three I would suggest you use the third option:

public class Addr(){
  static URL MyAddr;
}

public main() extends Applet{

....

   Addr something = new Addr();

   something.myAddr = getCodeBase();
...

}

opcion(){
...
   Addr something = new Addr();

   // Do something with something.myAddr

...

}
0
 

Author Comment

by:DaThiN
Comment Utility
Thanks a lot again, but I have other problem now. In Opcion init( ) method I have

button = getImage( urlCode.CodeBase, img_src );

where urlCode.CodeBase has the right URL of the code, and img_src rightly contains "Imagenes/boton.gif", file that does exist. After the call of getImage, button cantains null and after, when I try to add it to the MediaTracker, an exception is thrown (NullPointerException). I know that the exception occurres because button contains null after getImage( ). The fact that I cannot understand is that if I cut and paste the same line to the Menu init( ) method, it works. So I always have the same problems.. some methods works if I call them from the main class, and they don't if I call them from other class. I have read books and documentations and maybe I'm not realizing of something. Something may be left in my code.. I don't know.

Declarations of 3 classes are:

-----------------------------------
import java.net.*;

public class Address
{
      static URL CodeBase;
}
-----------------------------------
import java.awt.*;
import java.applet.*;

public class Menu extends Applet
{
        ........
      public void init( )
        {
        ......
        }

        public void paint( Graphics g)
        {
        ......
        }
}
---------------------------------------
import java.awt.*;
import java.applet.*;

public class Opcion extends Applet
{
        // Constructor
      Opcion( String i, String l, String f, int c, int o, String t, int g, String text ) {
                .......
            // Crea los botones
            create( );
      }

      protected void create( ) {
            MediaTracker t;

            if( off_im == null ) {
                  Address urlCode = new Address( );
                  try {
                        button = getImage( urlCode.CodeBase, img_src );
                        t = new MediaTracker( this );
                        t.addImage( button, 0 );
                        t.waitForID( 0 );
                  } catch( Exception e) {
                        showStatus( "Ha ocurrido el error en el Applet: " + e );
                  }
            }
                ...............
      }

      public void paint( Graphics g, int coord )
      {
            g.drawImage( off_im, 3, coord, this );
      }

}
----------------------------------------

Thanks in advance
Jaime
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now