Java string returns as null

前端 未结 4 1075
梦如初夏
梦如初夏 2021-01-28 01:46

I am attempting to get one class to return a string from another class, though the return I get is null. I have a set method that works in setting the string in the original cla

4条回答
  •  难免孤独
    2021-01-28 02:16

    In IceCream class constructor you have:

    String flavour = getFlavour()
    

    You have created a local variable instead a reference to a instance property. getFlavour() method return the property instance that you never set, so its null. You should have something like this in the constructor:

    this.flavour = "default value";
    

    Or set a flavour parameter on constructor header:

    public IceCream(String flavour) {
        this.flavour = flavour;
        (...)
    }
    

    And call it like:

    IceCream chocolat = new IceCream(" chocolat");
    

    And if you want change the flavour use the setter.

提交回复
热议问题