Skip to content

State Pattern/State Winner, has a bug in GumballMachine.java #16

@yousifAlneamy

Description

@yousifAlneamy

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions