0

Sorry as I know this is obvious but I cant figure it out!

I have a parent class named 'Set', representing a set of a tennis match.

public class Set {
private String set1;
private String set2;
private String set3;
//private Object[] match;

public Set() {
    setSet1(set1);
    setSet2(set2);
    setSet3(set3);
}

public void setSet1(String set1) {
    this.set1 = set1;
}

public String getSet1() {
    return set1;
}

public void setSet2(String set2) {
    this.set2 = set2;
}

public String getSet2() {
    return set2;
}

public void setSet3(String set3) {
    this.set3 = set3;
}

public String getSet3() {
    return set3;
}


public String toString(){
    return String.format("set1: %s, set2: %s, set3: %s", set1, set2, set3);
}


}

And a sub class of 'Set' named 'SingleSet', where i try to add the sets into an array named 'game':

public class SingleSet extends Set{
    private Object homePlayer;
    private Object awayPlayer;

    private String[] game;

    public SingleSet(Object homePlayer, Object awayPlayer){
        super();

        game = new String[3];

        game[0] = super.getSet1();
        game[1] = super.getSet2();
        game[2] = super.getSet3();

        setHomePlayer(homePlayer);
        setAwayPlayer(awayPlayer);
    }

    public void setHomePlayer(Object homePlayer) {
        this.homePlayer = homePlayer;
    }

    public Object getHomePlayer() {
        return homePlayer;
    }

    public void setAwayPlayer(Object awayPlayer) {
        this.awayPlayer = awayPlayer;
    }

    public Object getAwayPlayer() {
        return awayPlayer;
    }

    public void setGame(String[] game) {
        this.game = game;
    }

    public String[] getGame() {
        return game;
    }

    public String toString(){
        return String.format("Player: %s Vs. Player: %s, Single set game: %s, %s, %s", homePlayer, awayPlayer, game[0], game[1], game[2]);
    }
}

And this is where I am trying to add the Sets from my parents class into my sub class (this is for FXML, so the code is in my controller):

 public void submit() {
    SingleSet game1 = new SingleSet(homePlayer1Dropdown.getValue(), awayPlayer1Dropdown.getValue());
    game1.setSet1(set1Box1.getText());
    game1.setSet2(set1Box2.getText());
    game1.setSet3(set1Box3.getText());

    System.out.println(game1);

When I print the result, all my values are null. I tried printing them individually and that worked fine, so I know the 'set1Box.getText()' is working fine.

Again sorry for any obvious rookie error!

I've updated my code and same issue. Thank you for the composition answer, I will need it for my project, but this is a IS-A relationship

Codekid
  • 53
  • 5
  • Why does `SingleSet` inherit from `Set`? Anyway you're not setting `set` in `game[0]`, `game[1]` or `game[2]`, but in the current instance's copy of `set`. I think you are a little confused about how inheritance work and when to use it. – Federico klez Culloca Feb 28 '21 at 20:29
  • 1
    Your contructor Set() does notset anything, because it sets the not initilaized variable set to itself. Btw.: Don't nam your class like commonly used classes (i.e. Set) – juwil Feb 28 '21 at 20:32
  • Take a look at [this question](https://stackoverflow.com/questions/2399544/difference-between-inheritance-and-composition). It should help you see the difference between composition (which looks like what you actually want to achieve) vs inheritance. – Federico klez Culloca Feb 28 '21 at 20:32
  • setSet method is a setter for your "set" variable. in your to string method you wanna print game[0...2] variables. so you did not initialize them and you get Null. – behrad Feb 28 '21 at 20:43
  • There's a typo in the setter, should be `public void setSet(String set1) { this.set = set1;}` – Nowhere Man Feb 28 '21 at 20:51
  • Unrelated: it is almost always a bad idea when you start naming variables x1, x2, x3, ... that screams: use an array instead. These things are all "the same", so writing so many times the same code will quickly turn into maintenance problem for you. Also note: your default constructor is pointless. It calls your setters passing nulls into them. Your fields are all null by default, and your constructor doesnt do anything to change that. You should understand the effects of your code, and only write down that code that does something meaningful. – GhostCat Mar 02 '21 at 10:20
  • And it is always a critical thing to reuse names that Java already has in its standard libraries. So: consider to rename your class to TennisSets for example. "Set" alone is already misleading, as it contains 3 sets. Then: be careful about just using String as type all over the place. Very often, it might be more appropriate to create specific classes to express what a set really is. String objects have some advantages, but java is a compiled language, so the answer to specific problems is often to have specific types. Like: if your data is `5` then use int, not a String "5". – GhostCat Mar 02 '21 at 10:23
  • I really appreciate that really informative! Can I ask, do you know if in FXML it is possible to retrieve an int from a TextField? As it seems to have a problem with me casting. Also I want to retrieve two integers from a separate text field, separated by a ":", is that possible? lots of questions I know. Thanks again! @GhostCat – Codekid Mar 02 '21 at 10:27
  • You will probably need some sort of conversion. Yes, such input fields are string-based, but I would be surprised if there arent nice ways to automatically transform values. – GhostCat Mar 02 '21 at 10:29

1 Answers1

0

Make sure that the toString() methods of the following attributes exist and return a correct string. It seems as if there is no way to get a String from homePlayer, awayPlayer and all indices of game[x].

public String toString(){
        return String.format("Player: %s Vs. Player: %s, Single set game: %s, %s, %s", homePlayer, awayPlayer, game[0], game[1], game[2]);
    }
Shraft
  • 332
  • 1
  • 5