Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (512)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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 1048 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,
1  
2  
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.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
public void attack( Attack a, Monster target )
   {
      if( a == null )
         return;

      System.out.println( "\n" + this.name + " 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: 350
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.

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

JGO Kernel


Medals: 350
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: http://download.oracle.com/javase/1.4.2/docs/api/java/lang/AbstractMethodError.html 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: 350
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:
Quote
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: 350
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 www.jgrasp.org. 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.

theagentd (12 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org 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‑gaming.org
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!