Results 1 to 12 of 12
- 05-08-2011, 11:57 PM #1
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Main Method Can't Find It's Parent Class
I've been working out the kinks in this little project of mine for a few weeks, and now I'm trying to get the GUI working. I used NetBeans to make a GUI, and then copied the source code back into BlueJ (Which I'm in the process of growing out of).
When I try to compile the Battle class with the main method in it, I get this error: "cannot find symbol - constructor Battle()" which doesn't make any sense to me since it's inside the Battle class.
Here's the main method:
I thought maybe it had something to do with the fact that the class takes some parameters, and I put them into the method as well, which looked like this:Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Battle().setVisible(true); } }); }
but when I do that I get a ".class expected" error. What the heck is happening??Java Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new Battle(Player[] t, Player[] f, String i, String v).setVisible(true); } }); }
- 05-09-2011, 12:14 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Have you written a Battle() constructor?
- 05-09-2011, 12:21 AM #3
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Yes, I have:
Java Code:public Battle(Player[] t, Player[] f, String i, String v) throws java.net.MalformedURLException { team = t; foe = f; currentattack = "None"; intro = i; victory = v; x = 0; y = 0; currentteam = team[x]; currentfoe = foe[y]; initComponents(); }
- 05-09-2011, 12:23 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Can I see the exact error message please?
- 05-09-2011, 12:24 AM #5
Well... you should send variables to a constructor, not variable definitions.
- 05-09-2011, 12:28 AM #6
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
When I try to compile it without parameters: "cannot find symbol - constructor Battle()" on the line
With parameters I get ".class expected" on the same lineJava Code:public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { [COLOR="red"]new Battle().setVisible(true);[/COLOR] } }); }
- 05-09-2011, 12:31 AM #7
Well... of course. Your constructor expects certain variables. You don't have a constructor called Battle(), so you can't use it.
- 05-09-2011, 12:34 AM #8
Member
- Join Date
- May 2011
- Posts
- 8
- Rep Power
- 0
Oh, ok I think I understand now. The class expects to be given those parameters by some input. In that case I think I need the main method in another class. Thanks for your help!
- 05-09-2011, 01:58 AM #9
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
You don't need the main in another class, as long as it has access to the classes it is referencing. Please show me the exact error message, no paraphrasing(copy and paste it)
- 05-09-2011, 02:13 AM #10
Senior Member
- Join Date
- Mar 2011
- Posts
- 261
- Rep Power
- 3
When you're creating a new Battle object, you must include the parameters that you have required in your constructor.Java Code:public Battle(Player[] t, Player[] f, String i, String v)
A default parameter-less constructor ( like Battle() ) is only created if no constructors are coded. Otherwise you will have to create one yourself.
- 05-09-2011, 02:31 AM #11
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,561
- Rep Power
- 11
I think Toll was getting at the point that this should benew Battle(Player[] t, Player[] f, String i, String v).setVisible(true);
where the parameters are suitably initialised.Java Code:Player[] t = ... Player[] f = ... String i = .. String v = ... new Battle(t, f, i, v).setVisible(true);
- 05-09-2011, 02:37 AM #12
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
Wow, I feel dumb for not noticing that. Shows me not to skim so much.
Similar Threads
-
Could not find main class
By yetty4ever in forum New To JavaReplies: 7Last Post: 02-16-2011, 07:38 PM -
Running main method class from another main class
By tlrocketman in forum New To JavaReplies: 3Last Post: 12-06-2010, 08:30 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
Child-Class Calling a Method in a Parent-Class
By Blah_ in forum New To JavaReplies: 5Last Post: 09-29-2009, 02:48 AM -
problems to find the main method
By christina in forum EclipseReplies: 2Last Post: 08-06-2007, 07:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks