Click here to Skip to main content
15,846,346 members
Please Sign up or sign in to vote.
3.33/5 (3 votes)
See more:
Dears,
I have an Applet class, and I want to make it run as an application, so I wrote to following code:

public static void main(String args[]) {
JFrame app = new JFrame("Applet Container");
app.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
app.setSize(200, 100);
Hangman applet = new Hangman();
applet.init();
app.setLayout(new BorderLayout());
app.setSize(500,500);
app.getContentPane().add(applet, BorderLayout.CENTER);
app.setVisible(true);
}


Note: Hangman is the applet class. And if i run it, it work fine, but what I am tring to do, is to make it run as an application.
When I run the above main, I got the following error:

Exception in thread "main" java.lang.NullPointerException
at java.applet.Applet.getCodeBase(Applet.java:152)
at Me.Hangman.init(Hangman.java:138)
at Me.Client.main(Client.java:54)
Java Result: 1


This error is came from this line in the Hangman class:

danceMusic = getAudioClip(getCodeBase(), "../../audio/dance.au");


GetCodeBase() method returns null, I need help on how can I make this method work properly, or maybe replace it with another method that may access my files to get resources?
Thank you in advance
Posted
Comments
TorstenH. 30-May-11 6:52am    
what is getCodeBase() meant to return?
mostafa_darwiche 30-May-11 6:59am    
It is an URL, it pointer on yourporjectdirectory/build/classes

1 solution

Please read this interesting article here: Turning an Applet into a Standalone Application[^]. The part you should look into is the MyAppletStub code where an implementation is provided for the function that is causing you problems: getCodeBase().

I remember some stuff about applets as I used it in the mid 90s and I recall there was such a thing called an "Applet Runner" java application that allowed to let you run an applet outside of a browser. Maybe there's still something like that you could use.

Cheers!

-MRB
 
Share this answer
 
Comments
mostafa_darwiche 31-May-11 3:25am    
Thank you very much :)
Manfred Rudolf Bihy 31-May-11 4:51am    
I'm glad if I could help you! :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900