Results 1 to 13 of 13
Thread: Class can't be instantiated.
- 01-24-2012, 10:27 PM #1
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Class can't be instantiated.
Hello,
Im new into java and especially in applets.
I have two classes Say.java and SayWhat.java.
Say.java contains following code:
and SayWhat.java contains:Java Code:public class Say { SayWhat word = new SayWhat("hello"); }
When im trying to compile it, it throws me an error:Java Code:import java.applet.Applet; import java.awt.Graphics; @SuppressWarnings("serial") public class SayWhat extends Applet { Graphics g; public SayWhat(String what) { paint(what,g); } public void paint(String string, Graphics g){ g.drawString(string, 20, 20); } }
Can please someone explain me what am I doing wrong?load: SayWhat.class can't be instantiated.
java.lang.InstantiationException: SayWha
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)Last edited by 3maksim; 01-24-2012 at 10:58 PM.
- 01-24-2012, 11:07 PM #2
Re: Class can't be instantiated.
That error message looks like an execution error not a compile error.
What command are you executing when you get that error?
Where is the ending t on this: java.lang.InstantiationException: SayWha
- 01-24-2012, 11:08 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Class can't be instantiated.
That's not a compiler message, it's a runtime error message and it's occuring when your applet attempts to run (as part of a web page etc).
According to the first line of the error message you are attempting to use a class SayWha. It looks like you have made a typo and forgotten the last letter of the class. This is probably in the web page or wherever you specified the class to run.
- 01-24-2012, 11:14 PM #4
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Class can't be instantiated.
Typo is fixed now and actually im trying to run it in Eclipse at the moment. Problem still occurs and I cant figure it out why it is not working.
- 01-24-2012, 11:27 PM #5
Re: Class can't be instantiated.
Please post the full text of the error message.
- 01-24-2012, 11:41 PM #6
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Class can't be instantiated.
Full text of error message is in the first post, there is no more info.
load: SayWhat.class can't be instantiated.
java.lang.InstantiationException: SayWhat
at java.lang.Class.newInstance0(Unknown Source)
at java.lang.Class.newInstance(Unknown Source)
at sun.applet.AppletPanel.createApplet(Unknown Source)
at sun.applet.AppletPanel.runLoader(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
- 01-24-2012, 11:59 PM #7
Re: Class can't be instantiated.
Why does the SayWhat class extend Applet? Having the class extend Applet makes no sense for the way you are using it.
For a test, remove the extends part of the class definition and see what happens.
- 01-25-2012, 12:16 AM #8
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Class can't be instantiated.
hmm.
You extend an Applet to have an Applet.
Seems like im in a wrong place.
- 01-25-2012, 12:21 AM #9
Re: Class can't be instantiated.
If you want to execute an applet you use an applet tag in an HTML file to start its execution.
Your Say class (an application) is trying to create an instance of the SayWhat class.
Do you know how to write and execute applets?
See:Lesson: Java Applets (The Java™ Tutorials > Deployment)
- 01-25-2012, 12:22 AM #10
Member
- Join Date
- Jan 2012
- Posts
- 5
- Rep Power
- 0
Re: Class can't be instantiated.
Im executing the code in Eclipse.
- 01-25-2012, 12:43 AM #11
Re: Class can't be instantiated.
Sorry, I have no idea how your IDE works or how to use it.
If you need help with your IDE, there is a section of the forum for IDEs.
- 01-25-2012, 04:19 AM #12
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Re: Class can't be instantiated.
You can run applets in Eclipse using the "Run as..." command and selecting "Applet".
Your applet will not be able to be instantiated because it does not have a no argument constructor. It is not usual to define *any* constructor for an applet - and, in that case it will use the default no argument constructor.
Have a look at Java Applets lesson in Oracle's Tutorial and especially the sections on the so called "milestone" methods to see what methods you have to write for an applet and how you would use them to provide the applet with specific behaviour (like printing a specific word). Note that this lesson uses JApplet.
- 01-25-2012, 05:23 AM #13
Re: Class can't be instantiated.
Also, for an Applet or any other visual component, that painting code and instance Graphics variable are utterly wrong. find the section of the tutorial titled 'Performing Custom Painting' and learn from there.
dbWhy do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Eclipse Compile Error: Call validateValue(Class<T>, String, Object, Class<?>...)
By Tomshi in forum EclipseReplies: 0Last Post: 03-27-2011, 05:49 AM -
Circle is abstract; cannot be instantiated
By hoangx87 in forum New To JavaReplies: 20Last Post: 03-14-2011, 08:29 AM -
accessing variable/methods of a instantiated object
By hariza in forum AWT / SwingReplies: 5Last Post: 10-11-2010, 01:16 AM -
class is abstract; cannot be instantiated
By Jamison5213 in forum New To JavaReplies: 3Last Post: 04-25-2010, 09:16 PM -
NullPointerException even when class is instantiated as expected
By DCC1 in forum New To JavaReplies: 17Last Post: 07-12-2009, 06:42 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks