Results 1 to 10 of 10
Thread: No Such Method Error - simple
- 05-03-2010, 10:49 PM #1
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
No Such Method Error - simple
Hi.
This is my very first time writing java codes and I'm trying to build a small program to calculate the followin sum:
Sum = 30 + 60 + 120 + 240 + 480 + 960.
Here's the code that I came up with :
the compilation works fine.public class numA {
static int sumCal(int sum) {
int value = 30;
do {value *= 2;
sum += value;
}while (value < 960);
return sum;
}
}
but when I try to run it, this following message comes up:
I can't seem to find the problem... can anyone help?:(--------------------Configuration: <Default>--------------------
java.lang.NoSuchMethodError: main
Exception in thread "main"
Process completed.
- 05-03-2010, 11:03 PM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 4
every class you try to run must have a main method:
Java Code:public static void main(String[] args) { // this is the entry point into your program }
- 05-03-2010, 11:08 PM #3
if you want to run your class directly, the class must contain the main method. try this version:
Java Code:public class numA { static int sumCal(int sum) { int value = 30; do { value *= 2; sum += value; } while (value < 960); return sum; } public static void main(String[] args) { System.out.println(numA.sumCal(10)); } }
you can call your method without an instance of numA because the method is static.
think about it: when you start an application with multiple classes where should the jvm know where to start? right, the jvm is looking for a main method to know where to start and if the jvm doesn't find one an exception is thrown.
- 05-03-2010, 11:12 PM #4
- 05-03-2010, 11:41 PM #5
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
thank you alot.
your suggestion worked great.
do you guys have a page or a link I could check to understand more the basic of java?
my class notes aren't very useful since the basic like static method and such isn't explained that well.
another question:
public static void main(String[] args) {
System.out.println(numA.sumCal(10));is the method here "out.println" ?every class you try to run must have a main method
Here's another suggestion where I tried switching to Void all together and it didn't work out.
thanks alot :)public class lollaala {
public static void main(String[] args) {
int value = 30, sum;
do {
value *= 2;
sum += value;
} while (value < 960);
System.out.println("the sum is:" + numA.sum(10));
}
}
- 05-03-2010, 11:54 PM #6
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
There are two reasons why this didn't work.
- You no longer have a class called numA
- Your class does not have a sum() method
If you are going to put all of your code into main() then you just need System.out.println(sum);
By the way, letting your sum variable be implicitly initialized to zero like you did is not good style. You should make it a practice to explicitly initialize your variables. It's fine to declare them one place and initialize them somewhere else, but you should explicitly initialize them.
-Gary-
- 05-04-2010, 12:07 AM #7
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
When j2me64 talks about a "main method" he means a method specifically called main(). Really, it's a public static void method called main() that takes a String[] parameter (and the parameter name is generally "args"). So this:
Since you asked about System.out.println(), though, "out" is a PrintStream object that represents your program's standard output (the console) and is a static field of the System class. The PrintStream class has the println() method.Java Code:public static void main(String[] args) { }
-Gary-
- 05-04-2010, 11:17 AM #8
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
You could always try the Sun tutorials.
The Get Started section would be the place to start.
- 05-04-2010, 03:49 PM #9
Member
- Join Date
- May 2010
- Posts
- 10
- Rep Power
- 0
here's the code i came up with after you suggestion:
when i build it up i get this following error:public class numA {
public static void main(String[] args) {
int value = 30;
int sum;
do {
value *= 2;
sum += value;
} while (value < 960);
System.out.println(sum);
}
}
and I can't find the reason for this message to pop up since I did initialize Value & Sum.variable sum might not have been initialized
Question 2:
When j2me64 talks about a "main method" he means a method specifically called main(). Really, it's a public static void method called main() that takes a String[] parameter (and the parameter name is generally "args").What do you mean by "run your class directly"? when there is different classes in the same "public class numA", then i'm not running my class directly?if you want to run your class directly, the class must contain the main method.
I went ahead and tried to immerge 2 classes in the same public class numA.
Somehow System.out.print was not recognizing the variables.
and i get this following messagepublic class numA {
static int sumCal1(int Sum1) {
int value = 30;
do {
value *= 2;
Sum1 += value;
} while (value < 960);
return Sum1;
}
static int sumCal2(int Sum2) {
int value2 = 40;
do {
value2 *= 2;
Sum2 += value2;
} while (value2 < 1280);
return Sum2;
}
public static void main(String[] args) {
System.out.println(numA.sumCal1 (10), numA.sumCal2 (10));
}
}
I went ahead and googled that and it turns out there is 2 reasons for this error. Either I misspelled the name of the class or I didn't "Imported" the class name.cannot find symbol method println(int,int)
I think I didn't import the class name whatever importing means...
Also, what does (10) means?
Thanks for your patience everyoneSystem.out.println(numA.sumCal(10));
- 05-04-2010, 04:05 PM #10
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
For your first question, sum isn't initialised.
"+=" requires the thing on the left to be initialised already, so "sum += value" in your case gives you the error because sum has no value.
For question 2, take a step back. Every Java program needs an entry point. This is what main(String[] args) is. It is what java looks for in the class you call when it starts. So:
java MyClass
will cause java to look for MyClass, and will expect to find a main() method in there matching the pattern:
That's all people are saying.Java Code:public static void main(String[] args){}
As for println(). Get to know the API documents. They are your best friend.
System dot out dot println.
As you can see there are several println() methods, none of which has two int parameters.
Similar Threads
-
Simple Error ???
By darkblue24 in forum New To JavaReplies: 3Last Post: 03-26-2010, 08:15 AM -
Debuggin help - simple program non-static method errors
By RR_QQ in forum New To JavaReplies: 5Last Post: 02-04-2009, 01:20 AM -
[SOLVED] Simple Scanner Method - Plus Sign throwing me off...
By Josh.Hoekstra in forum New To JavaReplies: 2Last Post: 06-02-2008, 10:24 PM -
Help with a very simple method for a very simple beginner.
By cakeman in forum New To JavaReplies: 2Last Post: 05-04-2008, 05:27 PM -
Simple Method Question
By Froz3n777 in forum New To JavaReplies: 2Last Post: 02-13-2008, 02:39 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks