Hi !
Featured games (85)
games approved by the League of Dukes
Games in Showcase (623)
Games in Android Showcase (176)
games submitted by our members
Games in WIP (676)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  abstractMethodError  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline 2NickPick

Senior Newbie

« Posted 2011-02-14 05:45:55 »

OK so...

the attack() method is inside the Monster class

class Attack is an abstract class. It has MANY subclasses( so far about 10, planned to exponentiate ). The idea is, that attacks cause damage( or don't ), and also have some other effect in the battle such as raising its own attack power or lowering an enemy's defense power. So, class Attack has an abstract method called runEffect( ), which each subclass is forced to override.

For example,
Monster m = new Monster( );
m.attack( new SwordSlash( ), badGuy );

SwordSlash not only does damage, but with a 50% chance it can also lower the target's defense. So its runEffect() method runs the math, if the Monster is lucky, he lowers his target's defense.
public void attack( Attack a, Monster target )
      if( a == null )

      System.out.println( "\n" + + " used " + a.getName( ) + "!" );

      int damage = 0;
      if( a.getPower( ) != 0 )
         damage = calcDamage( a, target );

      target.takeDamage( damage );

      a.runEffect( target ); //AbstractMethodError
      a.decrementUsesLeft( );
      this.setAttackChoice( null );

I understand the error. SwordSlash is being demoted to just an Attack, and a.runEffect( ) is trying to use the runEffect( ) method in Attack which is abstract, hence the error. The question is what now? I don't want to write a different attack( ) method for each subclass of Attack.
Offline ra4king

JGO Kernel

Medals: 404
Projects: 3
Exp: 5 years

I'm the King!

« Reply #1 - Posted 2011-02-14 06:13:22 »

I have never seen that error before in my entire Java life. And I thought I've seen them all.
Could you care to print the entire stack trace?
Plus is this a runtime or compiletime error?

Offline 2NickPick

Senior Newbie

« Reply #2 - Posted 2011-02-14 06:37:06 »

EDIT: I understand the concept behind what is going on... if the method were

Monster.attack( SwordSlash, Monster )

then this would work fine. The idea I'm missing is why even bother using an Attack parameter if I can't use its subclasses methods.

There isn't much to the stack trace...

It's a runtime error( sorry, I should have mentioned that. In my head it was a compiler error ).
The runtime error occurs when the program comes across nonstatic Monster.attack( Attack, Monster ).

The Battle class essentially accepts two Monsters and loops them through a series of attacks until one's health is 0.
The battle loop is currently in the constructor of the Battle class... bad, I know.

Exception in thread "main" java.lang.AbstractMethodError: Attack.runEffect(LMonster;)V
    at Monster.attack(
    at Battle.<init>(
    at Battle.main(
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ra4king

JGO Kernel

Medals: 404
Projects: 3
Exp: 5 years

I'm the King!

« Reply #3 - Posted 2011-02-14 06:56:45 »

That is supposed to work fine with polyphormism and all.
Try recompiling all your java files and re-running it.
If you still get the error, could you paste the code for your Action, SwordSlash and Monster?

Offline lhkbob

JGO Knight

Medals: 32

« Reply #4 - Posted 2011-02-14 07:00:42 »

Java supports letting you write attack(Attack a) and passing in a SwordSlash and having it run the correct runEffect() method.  Normally the compiler should catch things like that.  If you look at the API for the exception: it sounds like you have a problem with an out-of-date .class file in your application.  If you're using Eclipse try doing a clean build, if you're compiling manually with javac make sure you recompile both Attack, SwordSlash and Monster.

<EDIT> Gah ra4king basically just said this, but I'm posting it anyway since there's the URL to the exception page</EDIT>

Offline ra4king

JGO Kernel

Medals: 404
Projects: 3
Exp: 5 years

I'm the King!

« Reply #5 - Posted 2011-02-14 07:08:31 »

The thing that screamed at me that this was an out-of-date class file is the V at the end of the stack trace. Without going into too much detail, that basically signifies incompatible methods.

EDIT: From the AbstractMethodError page:
this error can only occur at run time if the definition of some class has incompatibly changed since the currently executing method was last compiled.

Offline 2NickPick

Senior Newbie

« Reply #6 - Posted 2011-02-14 08:16:39 »

classic n00b mistake. Thanks ra4king (and lhkbob). The problem was runEffect() in Attack's subclasses. They all had an undefined method in them, which made them unusable by the compiler so it went up to the parent class version which was abstract hence, the AbstractMethodError.

I use JCreator, because my first professor used it and I've grown accustomed to it. Eclipse and netbeans both take extremely long to startup and I don't consider myself in need of their advanced features just yet. Everytime I build a project in JCreator, it saves all of the files that are open in JCreator. I thought it compiled them as well, but lesson learned. Thanks everybody.
Offline ra4king

JGO Kernel

Medals: 404
Projects: 3
Exp: 5 years

I'm the King!

« Reply #7 - Posted 2011-02-14 08:25:40 »

Understandable Smiley

By the way, I recommend using JGrasp It is an excellent IDE made in Java for Java Tongue
I use it and it is way simpler and easier to use Smiley

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

BurntPizza (14 views)
2015-10-07 02:11:23

KaiHH (13 views)
2015-10-06 20:22:20

KaiHH (11 views)
2015-10-06 19:41:59

BurntPizza (23 views)
2015-10-06 19:04:48

basil_ (44 views)
2015-09-30 17:04:40

shadowstryker (22 views)
2015-09-29 15:55:06

TheSpaceHedgehog (28 views)
2015-09-29 01:58:48

GamerC4 (54 views)
2015-09-24 21:10:38

GamerC4 (71 views)
2015-09-24 21:09:48

htuy (28 views)
2015-09-24 04:57:24
Math: Inequality properties
by Roquen
2015-10-01 13:30:46

Math: Inequality properties
by Roquen
2015-09-30 16:06:05

HotSpot Options
by Roquen
2015-08-29 11:33:11

Rendering resources
by Roquen
2015-08-17 12:42:29

Rendering resources
by Roquen
2015-08-17 09:36:56

Rendering resources
by Roquen
2015-08-13 07:40:51

Networking Resources
by Roquen
2015-08-13 07:40:43

List of Learning Resources
by gouessej
2015-07-09 11:29:36 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!