Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

Problem with getCodeBase()

Hello there.

This is driving me mad so any help would be very appreciated. I have an Applet written using Java Workshop. Because of this the class extends Group and therefore I cannot inherit from the Applet class. So I am using the getApplet() function to get around this.
What I need to do is connect the applet through a socket to a server application. This works fine but only when the applet is running on the same machine as the server (I use the getLocalHost() method to get the IP address). If I try to get the hostname using the getApplet() method as follows:
<Applet applet = getApplet();>
and then calling:
I get a nullPointerException. I know that getApplet returns null if not run through a browser but I'm running it through netscape.
Is there something similar to the getByName() method I can use to connect to the server? I tried getByName() but got another error (could not connect to xxx.xxx.xxx.xxx, x's being the IP address.)
As I said already, ANY help would be great.

Thanks in advance,

1 Solution
1. Easiest solution: pass (Applet a) in your class constructor.
2. In your code above you use getApplet()
   I suppose you mean getApplet(String name)?
   and in next row you need getCodeBase with ()?
3. Familiar problems described in Dippy Bird's help
   Problem is in BUGS in JDK 1.02.
   How to fix it you will find below. I included all
   text of this tip as it was in original document.

If you have two applets on the same page, they can access each other, i.e.,get a handle and then call methods in each other, etc.  

I orginally tried to use the example from the "Java in 21 days" book (page 299) where you give the applets names, say "Sue" and "Joe", then in the "Joe" applet you can call:  

   Applet sue = getAppletContext().getApplet("Joe");

I've yet to get this to work. First, you have to get past the bug in the Applet class for the getAppletContext() method. It is coded as:  

    public AppletContext getAppletContext() {
        return stub.getAppletContext();

Well, when the "Joe" applet's run method is called, it doesn't have a stub yet. Hence, if you call getAppletContext() you'll take a null pointer exception. Eventually, stub gets set and all is well with getAppletContext().  

However, I still can't find the named applet in the context. Hence, I resorted to cruising through all the applets on the page and testing to see if the current one is the one I'm looking for. For example:  

    // Wait until this.stub becomes non-null

    ac = getAppletContext();
    if (ac != null) {
      for (Enumeration e = ac.getApplets(); e.hasMoreElements();) {
        a = (Applet) (e.nextElement());
        if (a.isThisTheOneIAmLookingFor()) {
          yourHandle = (typeOfApplet) a;

You can now use yourHandle to call methods in the other applet.  

Pretty neat and useful. I'm using it to access the data that a data collector applet is responsible for collecting. In my case, I have the data collector applet which is collecting network CPU data from a seperate (non http) server, and two other applets which are dynamically displaying that data.  

I didn't make any cahnges for your case specially,
As you posted 2 rows of code with 2 errors
I only can make guesses what you really have!
so I do not want to write java code based on my guesses
about your code :)
but I believe you have the same bug problems.

fcurtinAuthor Commented:

Thanks for your reply.

I think you misunderstood my problem. The first line of code does not in fact contain an error. I am not using the getApplet() method of getAppletContext(). This is because to access getAppletContext() your applet must extend Applet. I can't do this as my applet already extends Group.
The getApplet() method I am using does not take a String arguement, it simply creates a new object of type Applet (in my case called applet) which allows you to access the methods of the Applet class.
The second line had an error - typo, sorry. I can see some similarities between my problem and the one you point out. However, I don't see how to use them to solve my problem. Perhaps there is a way and I'm just missing it??

Again, cheers for your help and any furthur advice would be excellent.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now