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 class, but when calling the method in my second class, I get a return of null.
Here is the first class;
public class IceCream
{
// instance variables - replace the example below with your own
private String flavour;
public static double price;
/**
* Constructor for objects of class IceCream
*/
public IceCream()
{
// initialise instance variables
String flavour = getFlavour();
price = 0.50;
}
/**
* Gets price in pence.
*
*
* @returns the price of the ice cream.
*/
public static double getPrice()
{
// put your code here
return price;
}
public int getScoops()
{
return scoop;
}
public void setPrice(int newPrice)
{
price = newPrice;
}
public void setScoops(int scoopNumber)
{
scoop = scoopNumber;
}
public double totalCost()
{
double cost;
cost = scoop * price;
return cost;
}
public String getFlavour()
{
return flavour;
}
public void setFlavour(String whatFlavour)
{
flavour = whatFlavour;
}
}
And the second class, in which I am trying to call the string I input in the setFlavour method in the println of the sundaeDetails method.
import java.util.ArrayList;
/**
* Write a description of class Sundae here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Sundae
{
// instance variables - replace the example below with your own
private IceCream flavour;
private Topping SundaeTopping;
private int scoops;
/**
* Constructor for objects of class Sundae
*/
public Sundae()
{
flavour = new IceCream();
SundaeTopping = new Topping();
scoops = 0;
}
/**
* Set scoop number.
*/
public void setScoops(int scoopNumber)
{
scoops = scoopNumber;
}
/**
* Return scoop variable.
*/
public int getScoops()
{
return scoops;
}
/**
* Get the price of the sundae.
*/
public void getPrice()
{
double cost;
double scoopPrice = scoops * IceCream.getPrice();
if ( scoops > 0) {
cost = scoopPrice * Topping.getToppingPrice();
System.out.println("Cost of Sundae: " + cost);
}
else {
System.out.println("Need to have a scoop of ice cream in your Sundae.");
}
}
/**
* Return the details of the sundae; price, flavour, scoops etc.
*/
public void sundaeDetails()
{
System.out.println("You have " + scoops + " scoops of " + flavour.getFlavour() + "ice cream");
}
}
The problem starts in the constructor of your IceCream class.
The first problem is that you are shadowing your member variable
flavour
- you are declaring another variable with the same name but whose scope is restricted to the constructor. The second problem is you are assigning the value returned bygetFlavour()
, which is just an accessor that returns the member variable itself (whose initial value is null).To fix it you need to assign an initial value in the constructor
Its also possible to assign the initial value when you declare it
Flavour is not initialized
private String flavour;
as anything, when callingIceCream()
you setString flavour = getFlavour()
, which sets the local variableflavour
inside the constructor to null, becausegetFlavour()
returnsnull
. Also the localString flavour
variable inside the constructor overshadows theString flavour
field of yourIceCream
class. Try making flavour a parameter of the constructor and then setting the field to that stringor call
setFlavour()
before you work with the object you created.You never call
flavour.setFlavour()
, so it's no surpriseflavour.getFlavour()
returns null.In IceCream class constructor you have:
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:
Or set a flavour parameter on constructor header:
And call it like:
And if you want change the flavour use the setter.