1

I'm creating a text adventure game in Java. I've created an exit, room, creature and an item class, and now I'm instantiating and putting values for them in my main class.

I'm getting this problem though: as I'm creating exits for my room, I'm getting a compiler error saying

Constructor Exit in class Exit cannot be applied to given types

This is the line of code I'm getting the compiler error for:

Exit exit1 = new Exit("Exit1", "Exit description", "Exit transition text");

and in my Exit class I have this constructor method:

public void Exit(String exit, String exitDescription, String exitTransition) {
    this.Exit(exit, exitDescription, exitTransition);       
}

but every time I tell the IDE (Netbeans) to correct the compiler error, it generates this in my Exit class:

Exit(String exit1, String exit_description, String exit_transition_text) {
    throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

So my question is, why doesn't my constructor method work? Also, I had help from my professor and I believe he said that I don't want the generated code. Don't remember exactly though.

Pang
  • 9,564
  • 146
  • 81
  • 122
noct27
  • 23
  • 2
  • 2
    `public void Exit` isn't a constructor, it's a method – MadProgrammer Oct 10 '15 at 02:23
  • 1
    Please read the help/mcve document and supply the missing pieces. You might also back up to the previous working version of the code and try moving forward just a couple of lines at a time. We can't help too much without the minimal, complete, and verifiable example (mcve). – Prune Oct 10 '15 at 02:32

1 Answers1

4

This is not a constructor:

public void Exit(String exit, String exitDescription, String exitTransition) {

constructors have no return type.

This is a constructor:

public Exit(String exit, String exitDescription, String exitTransition) {

Of course you know that even if this were a valid constructor the code in its body doesn't make much sense as it appears you're trying to have the constructor call itself recursively:

// void removed
public Exit(String exit, String exitDescription, String exitTransition) {
    this.Exit(exit, exitDescription, exitTransition); // this calls itself!
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • You could also say something about the body of the constructor. I'm not sure what the OP is trying to do there. – Paul Boddington Oct 10 '15 at 02:25
  • Okay thanks that helped me so much, so in the scope of this method do I put anything there? Or keep it blank since I will be doing all that when I call the method inside my main class? – noct27 Oct 10 '15 at 02:27
  • removing the void return statement made all the compiler errors disappear so, ill go read the constructor method chapter again tonight to get a better understanding – noct27 Oct 10 '15 at 02:29
  • 1
    @noct27 thing is that constructor is not a method http://stackoverflow.com/questions/19061599/methods-vs-constructors-in-java – Pshemo Oct 10 '15 at 02:31
  • @noct27: yes, there's no such thing as a "constructor method". There are constructors and there are methods, and you don't want to confuse the two. – Hovercraft Full Of Eels Oct 10 '15 at 02:32
  • @PaulBoddington: this is community wiki answer, so no one gets rep for it (I felt that the question was too basic for me to get rep for this answer), and all can improve it. So please pitch in. – Hovercraft Full Of Eels Oct 10 '15 at 02:32
  • public Exit(String exit, String exitDescription, String exitTransition){ } that is what I have in my Exit class, its a constructor and not a method because constructors are not methods, they're two different things. Am I correct? – noct27 Oct 10 '15 at 02:34
  • 1
    @Hovercraft I would do but I'm using my mobile which makes answering questions almost impossible. I'd have made this a community wiki answer too. – Paul Boddington Oct 10 '15 at 02:36
  • Dont know what a community wiki answer is, will it help me get more answers? Dont want people talking to each other on my question like a chat page, rather get productive answers. – noct27 Oct 10 '15 at 02:42
  • 2
    @noct27: You might not want to knock our talking as it's all in an attempt to improve this answer. You're asking for free advice, so please accept all you get in good faith and without being too critical. Note you will get better answers with better questions. To find out how to ask better questions please go through the [tour], the [help] and the [how to ask a good question](http://stackoverflow.com/help/how-to-ask) sections. – Hovercraft Full Of Eels Oct 10 '15 at 02:44
  • I understand, and will do, I fixed me compiler error so thank you guys for your input. Didnt mean to knock your talking, just couldnt tell if your "talking" was directed at me or not. I looked into my edit and saw my errors and will improve for next time, however I will be deleting this question because I resolved it. – noct27 Oct 10 '15 at 02:51
  • @noct27: you may try to delete this question, but you won't succeed. – Hovercraft Full Of Eels Oct 10 '15 at 02:54
  • @HovercraftFullOfEels If it doesnt need to be deleted it's fine with me, just dont want to waste people space on the screen. Sorry for being courteous – noct27 Oct 10 '15 at 02:57
  • but hopefully it can help people in the future, and maybe I can do something right for once in my life – noct27 Oct 10 '15 at 02:58
  • 1
    @noct27: I was just stating fact as you can't delete a question that has an up-voted answer. It's just one of the rules of the site. Also, note that a "community wiki" means that I don't get rep points for up-votes for this answer. This was per my choice. – Hovercraft Full Of Eels Oct 10 '15 at 03:07
  • @HovercraftFullOfEels I understand, well thank you for your help. You have helped a struggling computer science student. – noct27 Oct 10 '15 at 03:10