• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

main method not found, but it is clearly there

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 825
5
Python Ruby Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



And the error message even tells you that.
 
Bill Suttle
Ranch Hand
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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"?
 
Ranch Hand
Posts: 859
IBM DB2 Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your getTest() is returning a void.

WP
 
Kemal Sokolovic
Bartender
Posts: 825
5
Python Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 37
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic