Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

getCodeBase( ) and getDocumentBase( ) question

Posted on 1998-04-12
2
499 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
hibernate insert example 13 34
tomcat administrtor 12 61
check java version using powershell 13 171
Eclipse Java import and method not resolved 4 48
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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:
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…

856 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