Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Problem with getCodeBase()

Posted on 1997-08-15
2
Medium Priority
?
336 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
[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 10

Accepted Solution

by:
kollegov earned 100 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

Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
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 about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

722 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