I have been making a Tetris-like game in Java recently, I have a interface IBlocks which goes over ConsBlock and EmptyBlock (ConsBlock is a list with a block at the beginnning and a IBlock list at the end, for some reason that’s the way my teacher wanted it).
I have the getfirst() function in the interface:
public interface IBlocks{
//returns the first block in a list of blocks
public Resting getfirst();
(A Resting is one block in the Game)
Then, in the ConsBlock class, I have:
public class ConsBlock implements IBlocks{
//returns the first block in a list of blocks
public Resting getfirst(){
return this.first;
But in the EmptyBlock class, I want it to return something that would be similar to saying there isn’t one. I tried returning null, but that gave me a null pointer exception due to that fact that the function in the interface tells it to return a Resting. What would be the best way to represent an empty one without returning a Resting with random numbers?
Then
Further reading: http://en.wikipedia.org/wiki/Null_Object_pattern
Response to your comment: you still need to do:
Note that your
IBlocksandConsBlockclasses need to return anIRestingnot aRestingMake sure to read that wiki link!