Featured games (78)
games approved by the League of Dukes
Games in Showcase (427)
Games in Android Showcase (89)
games submitted by our members
Games in WIP (466)
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 931 times)
0 Members and 1 Guest are viewing this topic.
Offline 2NickPick

Senior Newbie

« Posted 2011-02-14 06: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: 322
Projects: 2
Exp: 4 years

I'm the King!

« Reply #1 - Posted 2011-02-14 07: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 07: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: 322
Projects: 2
Exp: 4 years

I'm the King!

« Reply #3 - Posted 2011-02-14 07: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 08: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: 322
Projects: 2
Exp: 4 years

I'm the King!

« Reply #5 - Posted 2011-02-14 08: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 09: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: 322
Projects: 2
Exp: 4 years

I'm the King!

« Reply #7 - Posted 2011-02-14 09: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.


Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (76 views)
2014-04-15 18:08:23

BurntPizza (68 views)
2014-04-15 03:46:01

UprightPath (80 views)
2014-04-14 17:39:50

UprightPath (66 views)
2014-04-14 17:35:47

Porlus (81 views)
2014-04-14 15:48:38

tom_mai78101 (105 views)
2014-04-10 04:04:31

BurntPizza (165 views)
2014-04-08 23:06:04

tom_mai78101 (261 views)
2014-04-05 13:34:39

trollwarrior1 (210 views)
2014-04-04 12:06:45

CJLetsGame (220 views)
2014-04-01 02:16:10
List of Learning Resources
by SHC
2014-04-18 03:17:39

List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30 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!