Results 1 to 13 of 13
  1. #1
    3maksim is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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:
    Java Code:
    public class Say {
    	SayWhat word = new SayWhat("hello");
    }
    and SayWhat.java contains:
    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);
    	}
    }
    When im trying to compile it, it throws me an error:
    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)
    Can please someone explain me what am I doing wrong?
    Last edited by 3maksim; 01-24-2012 at 10:58 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: Class can't be instantiated.

    Quote Originally Posted by 3maksim View Post
    When im trying to compile it, it throws me an error:
    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)
    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.

  4. #4
    3maksim is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Class can't be instantiated.

    Please post the full text of the error message.

  6. #6
    3maksim is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default 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)

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.

  8. #8
    3maksim is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Class can't be instantiated.

    hmm.
    You extend an Applet to have an Applet.
    Seems like im in a wrong place.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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)

  10. #10
    3maksim is offline Member
    Join Date
    Jan 2012
    Posts
    5
    Rep Power
    0

    Default Re: Class can't be instantiated.

    Im executing the code in Eclipse.

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.

  12. #12
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.

  13. #13
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,188
    Rep Power
    19

    Default 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.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Replies: 0
    Last Post: 03-27-2011, 05:49 AM
  2. Circle is abstract; cannot be instantiated
    By hoangx87 in forum New To Java
    Replies: 20
    Last Post: 03-14-2011, 08:29 AM
  3. Replies: 5
    Last Post: 10-11-2010, 01:16 AM
  4. class is abstract; cannot be instantiated
    By Jamison5213 in forum New To Java
    Replies: 3
    Last Post: 04-25-2010, 09:16 PM
  5. Replies: 17
    Last Post: 07-12-2009, 06:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •