0

So I have a class called character(), and my second class ChooseCharacterGUI() extends that class, but when I try to initialize ChooseCharacterGUI() to create the application I get the error "Implicit super constructor character() is undefined. Must explicitly invoke another constructor."

This is the ChooseCharacterGUI() class.

public class ChooseCharacterGUI extends character {

final String CharacterChoice;
private JFrame frame;
private JPanel ChooseCharacterMenu;
private JPanel Home;


// Launches  application.
public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ChooseCharacterGUI window = new ChooseCharacterGUI();
                window.frame.setVisible(true);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });
}

//Creates application 
public ChooseCharacterGUI() {
    initialize();
}

Anyone know how I can fix this? thanks.

Nemo
  • 15
  • 2
  • By invoking another constructor, as the error message suggests. – Robert Harvey Mar 15 '15 at 00:14
  • It seems that you character class does not have a default constructor, and you should explicitly call it's constructor in constructor of derived class – Vladislav Lezhnin Mar 15 '15 at 00:14
  • Your `character` class has no non-argument constructor, so you need to call the provided constructor explicitly. – Tom Mar 15 '15 at 00:15
  • There are at least 3 ways to fix it. (1) By writing a constructor for `character` with no arguments, (2) By writing a constructor for `ChooseCharacterGUI` that invokes a constructor of `character`, (3) by removing the words `extends character`. It is difficult to know without more information which is the right thing to do. – Paul Boddington Mar 15 '15 at 00:27
  • 1
    This is a common "Java newbie" problem; see link. The only other thing to add is that naming a class `character` is a Java style violation. But don't change it to `Character` because that is a standard class name ... and "borrowing" can lead to readability issues. – Stephen C Mar 15 '15 at 00:38

2 Answers2

0

Most likely your character class has a parameterised constructor and no default constructor. E.g.

class character {
     public character(String someargument) { ... }
}

So your CharacterGUI class needs to have in its default constructor a call to the parameterised constructor of the super class. E.g.

class CharacterGUI {
     public CharacterGUI() {
         super("somedefaultvalue");
     }
}

Perhaps another concern is that your super class has a lowercase first letter, which is confusing, and you're inheriting this in the class which also has the main function. This code might need a bit of refactoring when you've got it working.

Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23
  • This is the code from my character class - `public character (String iName, String iGender){` I just added this code to my ChooseCharacterGUI() constructor and it seems to have worked - `public ChooseCharacterGUI() { super("test", "Male"); initialize(); }` – Nemo Mar 15 '15 at 00:35
0

Every class you write must have a constructor, and every constructor must invoke super. If you don't write a constructor, Java generates one with no arguments that does nothing but call super(). If you write a constructor but don't have a super call, Java inserts an invisible super() at the top. So if either of these automatic things is happening and the super class doesn't have a no-args constructor (called a default constructor), you get this error.

So your character class must not have a default constructor. Either write one, or in your CharacterGUI class, call whatever character constructor you have.

I strongly suspect that you don't actually want to have CharacterGUI extend character though. And also, by convention, character should be capitalized.

MattPutnam
  • 2,927
  • 2
  • 17
  • 23