Solved

Problem with getCodeBase()

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
The viewer will learn how to implement Singleton Design Pattern in Java.

912 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