Solved

Problem with getCodeBase()

Posted on 1997-08-15
2
331 Views
Last Modified: 2011-10-03
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:
<applet.getCodeBase.getHost());>
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,

Fergus.
0
Comment
Question by:fcurtin
2 Comments
 
LVL 10

Accepted Solution

by:
kollegov earned 50 total points
ID: 1225088
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.

0
 

Author Comment

by:fcurtin
ID: 1225089
Kollegov,

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.

Fergus.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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
web services creation SOAP vs REST 5 56
spring jars download 1 35
ejb wildfly example 2 18
Java class and jar 3 20
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…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

808 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