• 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

what did i break? "no main method found"

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm trying to finish up some stuff here on the last day of classes. I have a couple last programs to write and am running into some problems. I wrote a program that needs a constructor, accessor, and mutator methods, and it basically takes in a couple strings and doubles and simply returns a value.

I don't know what I did, but all of a sudden on line #5, netbeans is saying that "class main is public, should be declared in a file named main.java"... Well, I'm in the file main.java. I don't get it, it starts off the same as all my other programs. Additionally, when I try to run it, it says it has no main method. Maybe you guys can see something that I am not?



I'm pretty sure it's something dumb.



Thanks!



 
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The main method must be static.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if i made it static, netbeans underlines line 15 and states: "non-static variable this cannot be referenced from a static content", which is why i removed static from it.


EDIT: nope, i was thinking about the wrong program, my bad. no, for some reason it still says no main method found. adding static makes line 15 not work and the program error out.
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
updated
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not line 15 - line 7. We're talking about the main method.

And it should go without saying that it's confusing -if not problematic- to have a class named X that contains a method named X.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"My carburetor was making a ticking sound so I removed it. The ticking sound is gone, but now the car won't start."

Solve the real problem rather than "solving" problems by patching symptoms. What is the exact error message you receive and on what line?
 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:"My carburetor was making a ticking sound so I removed it. The ticking sound is gone, but now the car won't start."

Solve the real problem rather than "solving" problems by patching symptoms. What is the exact error message you receive and on what line?




the problem is exactly as in my first post: i try to run the program, and it says "no main method found".
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Even after you put the static keyword back in?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Hofmann wrote:if i made it static, netbeans underlines line 15 and states: "non-static variable this cannot be referenced from a static content", which is why i removed static from it.



Generally, it is not a good idea to make a change without knowing why it was an error. And making a change to make the compiler not complain is rarely a good idea. For example, I can pull the battery from my car to get my car's computer to stop turning on the "check engine" light, but I don't think that would be a good idea...

[EDIT: Bear beat me to the example, and he used a vehicle example to boot]

Henry

 
Steven Hofmann
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bear Bibeault wrote:Even after you put the static keyword back in?



Yes, it still says no main method found, and it doesn't run the program. I'm really scratching my head on this, as I have maybe 15 programs from the semester that start the same way.
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
At this point you'll have to post the code.
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Steven Hofmann wrote:
I don't know what I did, but all of a sudden on line #5, netbeans is saying that "class main is public, should be declared in a file named main.java"... Well, I'm in the file main.java. I don't get it, it starts off the same as all my other programs. Additionally, when I try to run it, it says it has no main method. Maybe you guys can see something that I am not?



Double check that the class name matches the file name -- remember that Java is case sensitive. Double check that the package name matches the directory structure too.

Unfortunately, I don't know anything about netbeans, so can't tell if you corrupted something with the IDE specific files.

Henry
 
Ranch Hand
Posts: 31
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two issues here. First, as was already mentioned, you have to have the static keyword on your main method. The two methods

and


are two different methods because they have different signatures. If you don't have the static in the method signature, the classloader can't find main when it tries to run your program.

When you put static back in, you do get an error on line 15


This is because your class, Stock isn't static and is nested in the main class. In order to use it in the static main, Stock must be static as well. If you add the static keyword to the Stock class, your code will build and run. At least it did for me.


I would also agree with others here that you probably shouldn't name your class main. This is bad form and shouldn't become a habit when you are just learning java.

 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe more importantly, why is Stock an inner class of main? It doesn't need to be, and it rather looks like this was one more patch repair after the IDE complained about it being a public class in main.java.

Also, by convention Java class names start with an uppercase letter.
http://www.oracle.com/technetwork/java/codeconvtoc-136057.html

And I second Lester's recommendation to not have a class with a method of the same name. At the very least, there should be a difference of case. But a class should always have a meaningful name. Main isn't.
 
Alan Hampson
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Maybe more importantly, why is Stock an inner class of main? It doesn't need to be, and it rather looks like this was one more patch repair after the IDE complained about it being a public class in main.java.



Yup. Well I wasn't even going to go there!

You are correct though. It should be a separate class in it's own file.
 
Run away! Run away! Here, take this tiny ad with you:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic