Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

getCodeBase( ) and getDocumentBase( ) question

Posted on 1998-04-12
2
Medium Priority
?
522 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
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 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 …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

618 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