| View previous topic :: View next topic |
| Author |
Message |
Selphania
I watch Dragonball-Z

Joined: 06 Mar 2007 Posts: 15 Location: Behind you.
|
|
When i run this, i get an error i have never seen before , java.lang.NoSuchMethodError: main
Exception in thread "main"
i use eclipse SDK, and a up to date java, if anyone could please find out what is wrong i will be very thankful.
the program is below.
thanks for reading this post, please reply.
package Demos;
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
} _________________ This has been cleared out by Ichigo. Feel free to redo your sig, but the limit for images is one, and keep the size under a meg, or I will clean it out again. Thanks. |
|
| Back to top |
|
 |
ickyb0d
Evangelion is cool

Joined: 08 May 2006 Posts: 407 Location: Sniper Island... LOCK ON!
|
|
if that's your entire program, aren't you missing a main function? Something like what's below inside of your Rectangle class?
| Code: | public static void main(String[] args) {
//insert awesomeness here
} |
_________________
 |
|
| Back to top |
|
 |
Selphania
I watch Dragonball-Z

Joined: 06 Mar 2007 Posts: 15 Location: Behind you.
|
|
I am very new so sorry my noob knowlage of java. Where does the main funcion need to be? And what do you mean by "main function". DO you mean i need to have it spit out a calculation?
please help
thanks for answering _________________ This has been cleared out by Ichigo. Feel free to redo your sig, but the limit for images is one, and keep the size under a meg, or I will clean it out again. Thanks. |
|
| Back to top |
|
 |
ickyb0d
Evangelion is cool

Joined: 08 May 2006 Posts: 407 Location: Sniper Island... LOCK ON!
|
|
Yeah, i haven't done java in a LONG time, but what you have above is basically a class definition. If you want to execute a program, i believe Java looks for some "main" function within one your class(es) to execute. Since you have no main function, it doesn't know what to execute and that's probbaly why it's throwing an exception. So if this is the only code you're using in your project, then you're most likely just missing this main function.
Heh, i also may be completely wrong, i haven't done java in a long time. If i get some time, maybe i'll mess around and see what i can come up with and see why it isn't working.
You can put the main function anywhere inside your class. Something like...
| Code: |
package Demos;
public class Rectangle {
private int x, y;
private int width, height;
public Rectangle() {
this(0, 0, 0, 0);
}
public Rectangle(int width, int height) {
this(0, 0, width, height);
}
public Rectangle(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public static void main(String[] args) {
//insert awesomeness here
}
}
|
_________________
 |
|
| Back to top |
|
 |
MrVince
Kohai (後輩)

Joined: 13 Dec 2005 Posts: 909
|
|
Icky is right, so I won't repeat all of what he said.
You are creating a Rectangle class. The question I have for you is, what do you want to do with it? Do you need to make a test to test the functionality of the Rectangle class? If you just want it to compile, you don't need to have a main method. You would just go to the directory the java file is and type: | Code: | | javac Rectangle.java |
The problem is that you will not be able to test this class unless you either:
1. Create a test class that references the Rectangle class and has a main() method.
-or-
2. Create a main method in class Rectangle to test the functionality.
For all you non-techs reading this, what I'm basically asking him is no different than someone handing me a fork and saying "make it work" when there is no food nor plate around.
I can tell by the level of style of the code you have typed so far it's not yours. I'm guessing it's some starting code provided by your instructor. I don't mind helping you out on this assignment but you're going to have to figure some of this stuff out on your own. |
|
| Back to top |
|
 |
Selphania
I watch Dragonball-Z

Joined: 06 Mar 2007 Posts: 15 Location: Behind you.
|
|
Well to answer your question,
I was just experimenting with new classes and found a demo.
my java teacher is fairly new to java so i have trying to learn more on my own, i didnt even know i needed a main. Thanks for your help, both of you. _________________ This has been cleared out by Ichigo. Feel free to redo your sig, but the limit for images is one, and keep the size under a meg, or I will clean it out again. Thanks. |
|
| Back to top |
|
 |
DEATHGOD
Moderator


Joined: 21 Nov 2005 Posts: 1098 Location: Ohio
|
|
Ill post some examples tomorrow working with the GUI. _________________
 |
|
| Back to top |
|
 |
Tenchi
I watch Dragonball-Z

Joined: 16 Mar 2007 Posts: 12 Location: Kansas
|
|
Iv been in a java class for the last semester of my Sr. year in HS. the Main class is the Driver program basicly. you dont NEED it if you are simply making a class like rectange.
the way you would use a Main method, is basicly like this.
this is where this file ends, suppose that you have these classes already compiled. Rectangle *drawRectangle is a method of class Rectangle*, and Circle *where drawCircle is a method of class Circle*.
public class *file name*
{
public static void main()
{
Rectangle.drawRectangle(20,20,20,20);
Circle.drawCircle(20,20,20,20);
}
}
___________________________________
the main method here is just used as a way to controll the classes that are already made but you dont need it to simply create a class.
PS
i compiled your java program * w/o the package Demos;* the reason you were getting that error is because Java does not know wtf package Demos IS* using JCreator *a handy editor, ill provide a link for the free download* and it compiles fine and w/o error. however it wont DO anything unless you access it from another program with paramaters being sent down like above, but probably you wana create an object of the Rectangle class...... "Rectangle q = new Rectangle(x,y,length,width);" something like that
also you need some code to actually Draw the rectangle... here is a snidbit to let you draw it.
when you make the method, you NEED "Graphics g" in there or it wont do a thing =D
public void paint(Graphics g)
{
g.drawRect(x,y,width, height);
}
=D anyway, if you have some questions or need some help debugging just gime a shout. btw here is the link for JCreator, this handy little program just simplifies things for you and allows you to view applets and run programs w/o command prompt.
http://www.jcreator.com/download.htm
You want the JCreator LE version, that is the free version. tho you can BUY the pro version if ya really want to  |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|