Meaningless Drivel is fun!
The moose likes Beginning Java and the fly likes NoSuchMethodError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NoSuchMethodError" Watch "NoSuchMethodError" New topic


Troy Johnson

Joined: Sep 24, 2003
Posts: 20
Hello. When I try to run my program I get the following error message "Exception in thread "main"java.lang.NoSuchMethodError:main
My program will compile fine, but will display this message when I try to run it. However, when I try other programs they run fine. Does anyone have any suggestions or ideas. Thanks.
Here is my code

[ November 03, 2003: Message edited by: Troy Johnson ]
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
Hi Troy
I guess that you have named your .java file, right?
We have to do following changes to make it work,
1. Remove 'public' keyword from 'public class Player {' statement
2. Add 'public' keyword to 'class Player1' statement
(I guess both of these classes are written in the same file)
Try making these changes and compile/run see if it works.
Here, the issues is,
"java always tries to find main method in the 'public class' with the name we provide to the .java file. ie. if we have a file and public class Test in there then it will try to find main in Test class. Now, here you have Player and Player1, main is in Player1 but Player1 is not public and you might have named the file to So it is trying to find main method in Player which is not there hence the error.
Troy Johnson

Joined: Sep 24, 2003
Posts: 20
Hello. I made the changes you suggested. I ran the program and got the following error message this time: Exception in thread "main"java.lang.ArithmeticException:/by zero at Player1.main<>
S Rubia

Joined: Apr 06, 2003
Posts: 23
Hi Troy
sure u will get this error b/c u try to divide a no. with Zero,
so to avoid this divide in place of
you should try this,means start dividing by 1;
Troy Johnson

Joined: Sep 24, 2003
Posts: 20
Hello. Thanks for your help everyone. I was able to run the program without any error messages. However, I dont have anything printing or displaying on the screen. Am I doing something wrong. I am trying to get the program to display the player information as such: Tom Smith (average = 400.0) is out or Tom Smith (average = 400.0) gets a hit. Any other suggestions. Thanks again.
S Rubia

Joined: Apr 06, 2003
Posts: 23
well Troy the problem is in ur for loop
for( int i = 3; i < 9; counter++)
as a for loop starts it initialize i=3 then checks condition
if(i<9) so it is true
start execution fill the array wiyh player objs and then increments counter and then again checks the condition wheather i<9 or not so as there is no increment in i it will always be true ,and loop will never ended so instead of above try
for( int i = 3; i < 9; i++)
i think no need to increament counter in for loop if u want to increase
counter twice use simply
counter+=2; avg=(9/counter);
David O'Meara

Joined: Mar 06, 2001
Posts: 13459

"Knowledge Seaker",
Your display name is still not valid. You have been asked to change your display name to a valid value on several occasions. For an example, see here.
We require that your display name be two words: your first name, a space, then your last name. Fictitious names are not allowed.
Please edit your profile soon, since accounts with invalid display names get deleted.
I agree. Here's the link:
subject: NoSuchMethodError
jQuery in Action, 3rd edition