Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes main method not found, but it is clearly there Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "main method not found, but it is clearly there" Watch "main method not found, but it is clearly there" New topic
Author

main method not found, but it is clearly there

Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Hi,

I'm fairly new to Java. I'm currently working on creating and passing parameters to classes. The following code throws the following error in Eclipse:

Error is: Main method not found in class com.example.MusicStore.MusicStore, please define the main method as: public static void main(String[] args)

Here is the code for my main class:


Here is the code of 'Music' class:



I've looked all over the internet trying to figure out what is going wrong and am having no luck and it is driving me crazy. Thanks
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

That's not the correct signature of main method.

Edit: Check The main Method section of Lesson: A Closer Look at the "Hello World!" Application.


The quieter you are, the more you are able to hear.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Kemal Sokolovic wrote:That's not the correct signature of main method.


And the error message even tells you that.
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Ok, thanks. Yep the 'Object String' does not belong. That must have been something that Eclipse put in there after I clicked something for formatting stuff. Deleted the 'Object String'. Now, I'm getting:

Error: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

at com.example.MusicStore.MusicStore.main(MusicStore.java:48)

Which is for line 42. I'll work a bit and see if I can figure out what is going on -- it shouldn't be asking for a boolean, I'm assuming...as my getTest method is just to return the information from the Music class.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Once again, it's all in the error message. You are trying to print to console the result of getTest() method that doesn't return any value (check it's return type, it's void).
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Ok, my method getTest is not set up correctly. It looks like I will have to set up a distinct method for each type of data returned....String getArtist(), Int getYear() and so forth.

Thanks for the help...I should have looked more closely, but didn't even consider it as I didn't type the extraneous stuff in the main parameters!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tSuttle Dude wrote:Ok, thanks. Yep the 'Object String' does not belong. That must have been something that Eclipse put in there after I clicked something for formatting stuff. Deleted the 'Object String'. Now, I'm getting:

Error: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method println(boolean) in the type PrintStream is not applicable for the arguments (void)

at com.example.MusicStore.MusicStore.main(MusicStore.java:48)

Which is for line 42. I'll work a bit and see if I can figure out what is going on -- it shouldn't be asking for a boolean, I'm assuming...as my getTest method is just to return the information from the Music class.


When you call what value do you think will end up in the println(...) part?

To put it another way, if you did what value do you think would be assigned to x? 5? true? "abc"?
William P O'Sullivan
Ranch Hand

Joined: Mar 28, 2012
Posts: 859

Your getTest() is returning a void.

WP
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Why did you declare the fields of your Music class as static?
And Music doesn't sound to me like a good name for that particular class...
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Ok, I changed my getTest() in Music to:



and then I would assume the call from my main:



Would return whatever string I scanned in for 'album.' That is in fact not what happens..it returns nothing.
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Hi Kemal Sokolovic,

I didn't declare it as such. Eclipse did. I must have clicked something along the way that resulted in such. Yes, 'Music' is not the best name.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tSuttle Dude wrote:



Would return whatever string I scanned in for 'album.' That is in fact not what happens..it returns nothing.


That won't compile. And if you fix it so it does compile, it most certainly doesn't print out nothing unless you explicitly told it to print out nothing. What it will print out is the result of Object's toString() method, unless you have overridden toString() in whatever class the object pointed to by the result of getTest() is.
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Hi Jeff,

the code is actually: System.out.println( music.getTest() );

It will compile but it doesn't return anything. Any insight on why not in light of my above noted changes to getTest method?

I'm also now painfully aware that I've jumped the gun a bit and need to go back and write simpler programs until I really understand passing parameters to objects.

(note, I got rid of all of the static declarations to my Music fields)
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Hi,

I really appreciate the help. I've started fresh with a simple program that successfully accepted a string as a parameter and passed it to a class and then returned it via a method. Evidently my clicking on Eclipse stuff screwed the above program up with some extraneous stuff.

I'm going to go back to basics, practice, and do more reading. I shouldn't be jumping the gun trying to do things I do not really understand yet.

Thanks again!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

tSuttle Dude wrote:Hi Jeff,

the code is actually: System.out.println( music.getTest() );

It will compile but it doesn't return anything.


Well, it's true that println() doesn't return anything. We can tell that because its return type is declared as void. What it does do, however, is print its argument out to the console.

Any insight on why not in light of my above noted changes to getTest method?


Since getTest() is declared to return the String variable album, if nothing is coming out then (assuming you're not swallowing an exception somewhere) it means that album must hold the empty string. That is, "". This can only happen if you've explicitly set it to that value. Another possibility is that you're never actually calling getTest().

I'd suggest adding more println() statement to show you what code is getting executed in what order, and what values are being set.
Bill Suttle
Ranch Hand

Joined: Oct 22, 2012
Posts: 37
Hi,

Since you guys/gals helped me, I wanted to let you know that I rewrote the program from scratch WITHOUT Eclipse suggestions, read some syntax for how methods and return works, and everything is compiling perfectly. The only thing I can figure is that I went wrong somewhere and started clicking the Eclipse suggestions to add stuff without really understanding what I was adding, and after a few rounds of that the program was screwed.

Thanks!
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Awesome! I'm glad you took that approach, and were able to learn from it. And yes, you're probably spot on as to the source of the problem. An IDE is a great tool when you understand the foundations of what it's doing for you, but when you're new to the language (and new to that IDE, and especially if you're new to the concepts of programming and IDEs in general), you can do a lot of clicking without understanding what it does, and then when scientific progress goes boink, you've no clue what the problem is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: main method not found, but it is clearly there