Results 1 to 15 of 15
  1. #1
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default Cannot instantiate a type

    I'm doing a project with Eclipse and 1 error occurred in a class, I'll report the part of code where is signed:

    Nemico[] nemici=new Nemico[numNemici];
    for(int e=0;e<numNemici;e++){
    nemici[e]=new Nemico(vita,forza,destrezza,costituzione); //here!
    }

    the variable "numNemici" is istantiated in the costructor of the class (Arena in the specified case).
    "vita","forza","destrezza","costituzione" are private fields of the class (Arena).
    "Nemico" is the problem, but it's a costructor defined in the class Nemico:

    public Nemico(double vita,double forza,double destrezza,double costituzione){
    x=(int)Math.random()*100 //x e y are protected fields of the class Nemico
    y=(int)Math.random()*100
    this.vita=vita;
    this.forza=forza;
    this.destrezza=destrezza;
    this.costituzione=costituzione;
    //.... (other things XD)

    }

    So..where is the problem?Any idea?
    Eclipse just segnalized the problem, but don't suggest a solution.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    So..where is the problem?
    Can you compile it and have the compiler tell you where the error is?

  3. #3
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    the error is "Cannot instantiate the type Nemico", at the line where I wrote "here!" as a comment.But I don't undestand why Nemico cannot be instantiated._

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Can you use the javac command and get an error from the compiler
    or won't the IDE allow you to compile the program?

  5. #5
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    Sorry, I don't know Eclipse very well so I don't know how to compile a file with IDE..but I've copy the file in JEdit: the compiler as signed only the "normal" errors as "cannot find symbol" (normal because I haven't copy all the other classes in 1 folder, just the class Arena where the problem occurred)..

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    Hard to suggest anything without the full text of the error message.

  7. #7
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    I've read that to compile with IDE you can check "Build Automatically", the option is set on and the text of the error is "Cannot instantiate the type Nemico"..there aren't other messages.
    Clicking on "Quick fix" there arent'n suggestions to solve the problem.@_@

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    The IDE is hiding the compiler error. Try using the javac command to compile the program.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Switch off the Build Automatically option and do it manually via the Build button.
    I think (though it's been a while since I used it) that should give you a proper compilation.

  10. #10
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Tolls View Post
    Switch off the Build Automatically option and do it manually via the Build button.
    I think (though it's been a while since I used it) that should give you a proper compilation.
    I've tryed but the result is the same, I look in "Window->Show View->Problems"..it's right?Or where I can find what you're asking for?

  11. #11
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    maybe the problem is that Arena is'nt the main class and have not the main method?

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,874
    Rep Power
    25

    Default

    No, the compiler doesn't check for that.

  13. #13
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    mmh the problem is solved,was as well as i think.o.O Thanks

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,225
    Rep Power
    20

    Default

    Quote Originally Posted by Yeshol View Post
    I've tryed but the result is the same, I look in "Window->Show View->Problems"..it's right?Or where I can find what you're asking for?
    I thought there's an output panel down the bottom that all this sort of stuff goes to?

  15. #15
    Yeshol is offline Member
    Join Date
    Jul 2011
    Posts
    17
    Rep Power
    0

    Default

    I'm an idiot: the class was defined as "Abstract" =_='
    Now is ok.

Similar Threads

  1. How to instantiate Class, into jsp, java script
    By silvia in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 01-04-2014, 09:08 AM
  2. Replies: 0
    Last Post: 06-21-2011, 03:02 AM
  3. Internal error-- Cannot Instantiate Font Class??
    By MatthewC.Dowell in forum New To Java
    Replies: 1
    Last Post: 05-29-2011, 08:00 PM
  4. help instantiate a java class that extend applet
    By geosa in forum Java Applets
    Replies: 0
    Last Post: 12-04-2010, 10:14 AM
  5. [SOLVED] Unable to instantiate class?
    By xcallmejudasx in forum New To Java
    Replies: 1
    Last Post: 11-03-2008, 05:03 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
  •