• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

applet objects

 
tomas gussymurphy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you make an instance of an applet in another applet class and display it by calling its init() method?
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's nothing in the API to keep you from doing it. However, if you don't understand how applets work, you could throw a wrench in the gears. First thing you should do is answer Marilyn's question as to which line in your code is throwing the null pointer exception. You should also take a look at the Writing Applets chapter of the Java Tutorial to get up to speed on how these things work.
 
Shashank Agarwal
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You should be able to do this. Take care that both of them are in the same package or one of them is inherited by other.

package demo
|
--------------------------
| |
Applet1.class Applet2.class

package demo;
import javax.swing.*;

public class Applet1 extends JApplet
{
public void init()
{
Applet2.init();
}
}

I hope this works, tell me what happened.
 
tomas gussymurphy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
this is exactly what i was doing and gettig a null pointer error. however i have since come up with a hack work around and will be returning to the problem shortley. more later. thanks for your help. tom
 
tomas gussymurphy
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
according to your code above you have Applet2.init but you i dont see where you have created an instance of this Applet2 object and this is where i am going wrong. also you have some packeage information at the top. is this part of the code??
thanks.
tom
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note also that every applet has an instance of an object called the "AppletStub". This is the applet's connection to the browser, and it supplies services like displaying status text, reporting the codebase URL, etc. If you just create the applet by hand, the stub will be null and many of the applet's methods will end up throwing a NullPointerException.

It is possible to fix this: you have to call the newly-created applet's "setStub" method and pass it an instance of a class you've defined which implements the AppletStub interface; your AppletStub can be implemented mostly by calling back to the original applet's various methods, especially the getAppletContext() method.

This would be easier if Applet had a "getStub()" method, but it doesn't.
 
Joe Ess
Bartender
Posts: 9337
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shashank's code is wrong. You need to create an instance of Applet2 and use the instance rather than the class:
 
tom MURPHY
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i see, i think i was going wrong because i wasnt adding the applet object (just calling its init() method). maybe thats why i was getting a null pointer. will check this later...its night time here......and get back to you.
thanks for all the replies.
appreciate it and i hope it works,
tom
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic