Solved

Problem with getCodeBase()

Posted on 1997-08-15
2
327 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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Exchange and Third Party application. 8 60
pairstar challenge 2 42
json example 39 115
Starting to learn JAVA, 7 48
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
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…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now