Solved

getCodeBase( ) and getDocumentBase( ) question

Posted on 1998-04-12
2
485 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
ID: 1234061
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
ID: 1234062
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Loop 6 50
session migration servlets 2 28
factorial example 4 35
table example 4 24
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

867 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

21 Experts available now in Live!

Get 1:1 Help Now