Results 1 to 10 of 10
  1. #1
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default 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 :

    public class numA {

    static int sumCal(int sum) {
    int value = 30;

    do {value *= 2;
    sum += value;
    }while (value < 960);
    return sum;

    }

    }
    the compilation works fine.
    but when I try to run it, this following message comes up:

    --------------------Configuration: <Default>--------------------
    java.lang.NoSuchMethodError: main
    Exception in thread "main"
    Process completed.
    I can't seem to find the problem... can anyone help?:(

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    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
    }

  3. #3
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by n00b View Post
    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.
    but when I try to run it, this following message comes up:



    I can't seem to find the problem... can anyone help?:(

    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.

  4. #4
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    5

    Default

    Quote Originally Posted by iluxa View Post
    every class you try to run must have a main method

    you can also "run" a class A from an other class B. so the class A musn't have a main method.

  5. #5
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by j2me64 View Post
    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.
    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));
    every class you try to run must have a main method
    is the method here "out.println" ?

    Here's another suggestion where I tried switching to Void all together and it didn't work out.
    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));
    }
    }
    thanks alot :)

  6. #6
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by n00b View Post
    Here's another suggestion where I tried switching to Void all together and it didn't work out.
    Java Code:
    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));
            }
    }
    thanks alot :)
    There are two reasons why this didn't work.
    1. You no longer have a class called numA
    2. 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-

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by n00b View Post
    is the method here "out.println" ?
    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:
    Java Code:
            public static void main(String[] args) {
            }
    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.

    -Gary-

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    Quote Originally Posted by n00b View Post
    do you guys have a page or a link I could check to understand more the basic of java?
    You could always try the Sun tutorials.
    The Get Started section would be the place to start.

  9. #9
    n00b is offline Member
    Join Date
    May 2010
    Posts
    10
    Rep Power
    0

    Default

    Quote Originally Posted by gcalvin View Post
    There are two reasons why this didn't work.
    1. You no longer have a class called numA
    2. 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);
    here's the code i came up with after you suggestion:

    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);
    }
    }
    when i build it up i get this following error:

    variable sum might not have been initialized
    and I can't find the reason for this message to pop up since I did initialize Value & Sum.


    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").
    if you want to run your class directly, the class must contain the main method.
    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?

    I went ahead and tried to immerge 2 classes in the same public class numA.
    Somehow System.out.print was not recognizing the variables.

    public 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));
    }
    }
    and i get this following message
    cannot find symbol method println(int,int)
    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.
    I think I didn't import the class name whatever importing means...


    Also, what does (10) means?
    System.out.println(numA.sumCal(10));
    Thanks for your patience everyone

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,820
    Rep Power
    19

    Default

    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:
    Java Code:
    public static void main(String[] args){}
    That's all people are saying.

    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

  1. Simple Error ???
    By darkblue24 in forum New To Java
    Replies: 3
    Last Post: 03-26-2010, 08:15 AM
  2. Replies: 5
    Last Post: 02-04-2009, 01:20 AM
  3. Replies: 2
    Last Post: 06-02-2008, 10:24 PM
  4. Replies: 2
    Last Post: 05-04-2008, 05:27 PM
  5. Simple Method Question
    By Froz3n777 in forum New To Java
    Replies: 2
    Last Post: 02-13-2008, 02:39 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •