944,131 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 951
  • Java RSS
Oct 7th, 2009
0

How come it gives this error when I run a program?

Expand Post »
I copied this code from the textbook, so the code should have no mistake? I typed the contructor file(I think thats what its called) and the actual codes. So the code looks like this:

Java Syntax (Toggle Plain Text)
  1. public class TestCircle {
  2. public static void main(String[] args) {
  3. Circle spot = new Circle();
  4.  
  5. spot.setRadius(5);
  6.  
  7. System.out.println("Circle radius:" + spot.getRadius());
  8. System.out.println("Circle area: " + spot.area());
  9.  
  10. }
  11. }
And I saved it as TestCircle.java

the constuctor file:
Java Syntax (Toggle Plain Text)
  1. /**
  2.  * Circle class.
  3.  */
  4. public class Circle {
  5. private static final double PI = 3.14;
  6. private double radius;
  7.  
  8. /**
  9.   * constructor
  10.   * pre: none
  11.   * post: A circle object created. Radius initialized to 1.
  12.   */
  13. public Circle() {
  14. radius = 1; //default radius
  15. }
  16.  
  17.  
  18. /**
  19.   * Changes the radius of the circle.
  20.   * pre: none
  21.   * post: radius has been changed.
  22.   */
  23. public void setRadius(double newRadius) {
  24. radius = newRadius;
  25. }
  26.  
  27.  
  28. /**
  29.   * Calculates the area of the circle.
  30.   * pre: none
  31.   * post: The area of the circle has been returned.
  32.   */
  33. public double area() {
  34. double circleArea;
  35.  
  36. circleArea = PI * radius * radius;
  37. return(circleArea);
  38. }
  39.  
  40.  
  41. /**
  42.   * Returns the radius of the circle.
  43.   * pre: none
  44.   * post: The radius of the circle has been returned
  45.   */
  46. public double getRadius() {
  47. return(radius);
  48. }
  49. }
Saved it as Circle.java

When I compile it, it's fine.. no errors but when I go to see the ouput it gives me this:

> java Circle
Static Error: No static method in Circle with name 'main' accepts arguments (String[])

What does that mean, how do I fix it?
I think this happens to most of the programs I to run.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
StarZ is offline Offline
85 posts
since Dec 2008
Oct 7th, 2009
2
Re: How come it gives this error when I run a program?
It's very simple:
... the class Circle doens't have any main method. So it's not an actual "program".

You should use:
Java Syntax (Toggle Plain Text)
  1. javac Circle.java TestCircle.java
  2. java TestCircle

First line will compile both classes.
Second line will run the class containing the main method.

(don't skip the basics)
Last edited by nomemory; Oct 7th, 2009 at 6:11 pm.
Reputation Points: 19
Solved Threads: 6
Light Poster
nomemory is offline Offline
31 posts
since Sep 2009
Oct 7th, 2009
1
Re: How come it gives this error when I run a program?
What do u mean?
Like save it as that name?
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
StarZ is offline Offline
85 posts
since Dec 2008
Oct 7th, 2009
1
Re: How come it gives this error when I run a program?
nomemory means compile the classes then run TestCircle. You cannot run Circle directly as it has no main method. main is the entry point for you program. As nomemory said, don't skip the basics. Please mark your other thread as solved as well!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 7th, 2009
0
Re: How come it gives this error when I run a program?
oh ok, I got now.. got it to output.
So it's like instead of running the constructor file(circle) run the actual code program instead.. k thx. I'll mark the other one solve too but I can't find where it is, how do i find it.
Reputation Points: 11
Solved Threads: 1
Junior Poster in Training
StarZ is offline Offline
85 posts
since Dec 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: How come this program doesnt run?
Next Thread in Java Forum Timeline: Extracting Mouse icon of the system





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC