Results 1 to 12 of 12
  1. #1
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default 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:

    Java Code:
         
    public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    new Battle().setVisible(true);
                }
            });
        }
    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(Player[] t, Player[] f, String i, String v).setVisible(true);
                }
            });
        }
    but when I do that I get a ".class expected" error. What the heck is happening??

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Have you written a Battle() constructor?

  3. #3
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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();
           
        }

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Can I see the exact error message please?

  5. #5
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Well... you should send variables to a constructor, not variable definitions.

  6. #6
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    When I try to compile it without parameters: "cannot find symbol - constructor Battle()" on the line

    Java Code:
         public static void main(String args[]) {
            java.awt.EventQueue.invokeLater(new Runnable() {
                public void run() {
                    [COLOR="red"]new Battle().setVisible(true);[/COLOR]
                }
            });
        }
    With parameters I get ".class expected" on the same line

  7. #7
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    392
    Rep Power
    4

    Default

    Well... of course. Your constructor expects certain variables. You don't have a constructor called Battle(), so you can't use it.

  8. #8
    Slinkie45 is offline Member
    Join Date
    May 2011
    Posts
    8
    Rep Power
    0

    Default

    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!

  9. #9
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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)

  10. #10
    Solarsonic is offline Senior Member
    Join Date
    Mar 2011
    Posts
    261
    Rep Power
    4

    Default

    Java Code:
    public Battle(Player[] t, Player[] f, String i, String v)
    When you're creating a new Battle object, you must include the parameters that you have required in your constructor.

    A default parameter-less constructor ( like Battle() ) is only created if no constructors are coded. Otherwise you will have to create one yourself.

  11. #11
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    new Battle(Player[] t, Player[] f, String i, String v).setVisible(true);
    I think Toll was getting at the point that this should be

    Java Code:
    Player[] t = ...
    Player[] f = ...
    String i = ..
    String v = ...
    new Battle(t, f, i, v).setVisible(true);
    where the parameters are suitably initialised.

  12. #12
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

Similar Threads

  1. Could not find main class
    By yetty4ever in forum New To Java
    Replies: 7
    Last Post: 02-16-2011, 07:38 PM
  2. Running main method class from another main class
    By tlrocketman in forum New To Java
    Replies: 3
    Last Post: 12-06-2010, 08:30 AM
  3. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  4. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  5. problems to find the main method
    By christina in forum Eclipse
    Replies: 2
    Last Post: 08-06-2007, 07:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •