Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
{
//Draw
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)
return LEFTBOUND;
else if (x+width > xBound+w)
return RIGHTBOUND;
else if (y < yBound)
return TOPBOUND;
else if (y+height > yBound+h)
return LOWBOUND;
else
return INBOUND;

}

//Accessor
public int getWidth()
{
return width;
}
public int getHight()
{
return height;
}

//States

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;
}
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
Greetings,
Tim
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.
 
Danger, 10,000 volts, very electic .... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic