?
Solved

getCodeBase( ) and getDocumentBase( ) question

Posted on 1998-04-12
2
Medium Priority
?
516 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
[X]
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
2 Comments
 
LVL 6

Accepted Solution

by:
jpk041897 earned 40 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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month8 days, 23 hours left to enroll

765 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