This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

java.lang.NoSuchMethodError ???????

fred fredricks
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ppl! i have this code which i want to compile to see how it works..but its not compiling..i am a total newbie, so its probably really obvious..
i am trying to make a little image move up,down, left right when you press the arrow keys...any helps?
here is the code:
import java.awt.Graphics;
abstract class MoveableObject
abstract void draw(Graphics g);

//Move, specify the speed
public void move (int direction, int s)
if(direction == this.RIGHT)
this.x = x + s;
if(direction == this.LEFT)
this.x = x - s;
if(direction == this.DOWN)
this.y = y + s;
if(direction == this.UP)
this.y = y - s;

//Overloaded, use default speed
public void move(int direction)
move(direction, speed);

// True is this object is out of the given boundary
public int outOfBound(int xBound, int yBound, int h, int w)
if (x < xBound)
else if (x+width > xBound+w)
else if (y < yBound)
return TOPBOUND;
else if (y+height > yBound+h)
return LOWBOUND;
return INBOUND;


public int getWidth()
return width;
public int getHight()
return height;


protected int x;
protected int y;
protected int width;
protected int height;
protected int speed;

public static final int RIGHT = 0;
public static final int LEFT = 1;
public static final int UP = 2;
public static final int DOWN = 3;

public static final int RIGHTBOUND = 0;
public static final int LEFTBOUND = 1;
public static final int TOPBOUND = 2;
public static final int LOWBOUND = 3;
public static final int INBOUND = 4;
Tim B.
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Mike,
For as far as I can see, based on your code, I think the MoveabelObject class will compile. What you probably did is compile it and then 'run' it. When you would try to run this object you will indeed get a message much like: 'Exception in thread "main" java.lang.NoSuchMethodError: main'. This is because when you 'run' a java class, it will look for the 'main' method, which will then be executed. Your file does not contain a main method, hence the error.
What you should do is create another object, containing a 'main(String args[])' method. This method could instantiate your object and do all kinda other stuff that's required to actually show your object and let the user control it.
Ps. The code you posted is far from finished, atleast if you had in mind that the MovableObject is the only required file. It still contains an abstract method (draw) which you must implement, else nothing will happen.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic