yrstruly Posted June 17, 2009 Posted June 17, 2009 HelloIm having trouble running java programs on my machine. All of the codes im running is giving this error message:Exception in thread "main" java.lang.NoSuchMethodError: mainWhat am i doing wrong, should i update or install a new sdk? I dont understand what is wrong, cause sometimes the code run smoothly without any message like the one given. I can compile the code and its giving me no errors, but when i execute it then its a different story. I have included an example code./* Chapter 10: Debugging Assignment: ***: 78983D-E Programmer: Suret Delport (Student No.: 10007349767) Date: 28 April 2008 Filename: Circle.java Purpose: Circle class = object of TestCircle class*/ public class Circle extends Shape{ public int x=22, y=44; // coordinates of the center private double radius; // Constructors public Circle() { setRadius(0); } public Circle(double r, int x, int y) { x=22; y=44; setRadius®; } // Get radius of Circle public double getRadius() { return radius; } // Set radius of Circle public void setRadius(double r) { radius = ( r >= 0 ? r : 0 ); } // Calculate area of Circle public double area() { return Math.PI * radius * radius; } // convert the Circle to a String public String toString() { return "Circle with center at <", " + y + "> " + "and radius = " + radius; } // return the class name public String getName() { return "Circle"; } // return the center location public String getLocation() { return "X = "+x+", "+"Y = "+y+" "; }}
Synook Posted June 21, 2009 Posted June 21, 2009 There's no "main" method in that code... it isn't meant to be run, after all - you are supposed to import and use it in another piece of code. If you try to actually execute it, the interpreter looks for a function called "main" to run, can't find it, and throws an exception.You can incorporate it into an executable script, for example: class Example { public static void main(String[] args) { Circle circle = new Circle(3, 4, 5); System.out.println(circle.getLocation()); }}
Jack McKalling Posted June 24, 2009 Posted June 24, 2009 There is also a syntax error in your toString() method:return "Circle with center at <", " + y + "> " + "and radius = " + radius;The red code is what will also cause an error.
Recommended Posts
Archived
This topic is now archived and is closed to further replies.