-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Open
Description
state
property has null
value if the object was created with zero gumballs like: new GumballMachine(0)
Inside GumballMachine.java you have the following:
State soldOutState; // soldOutState is null
// other properties
State state = soldOutState; // state is null
public GumballMachine(int numberGumballs) {
soldOutState = new SoldOutState(this); // soldOutState has a new reference
// other code
this.count = numberGumballs;
if (numberGumballs > 0) { // if this is false, then state will still be null
state = noQuarterState;
}
}
So, to fix this it's better to add else
statement in the constructor to give state
the new soldOutState
value, like:
if (numberGumballs > 0) { // if this is false, then state will be soldOutState
state = noQuarterState;
} else {
state = soldOutState;
}
Metadata
Metadata
Assignees
Labels
No labels