1

What is the best way to check if a position is occupied or not? I don't think I should be using "this==null"...

class Cell {
    int column;
    int row;
    char letter;

    public Cell(int column, int row, char letter) {
        super();
        this.column = column;
        this.row = row;
        this.letter = letter;
    }

    public boolean isEmpty() {
        if (this==null) return true;
        else return false;
    }
}
ampc
  • 312
  • 1
  • 6
  • 17
  • 9
    `this` can *never* be `null` because it points to the current object. You need some other way to indicate that the `Cell` is empty such as another variable (a boolean, maybe?) – Code-Apprentice Apr 04 '13 at 00:47
  • Derek Greer, at http://lostechies.com/derekgreer/tag/tdd/, has a long set of examples on writing a tic-tac-toe game using TDD where he faces similar problems on how to render cells. However, it's in C#. I do recommend it anyway. – Eric Jablow Apr 04 '13 at 01:07

3 Answers3

2

I'm going to assume that the char is the content of your Cell and you want to check if that content is null.

First, this cannot ever be null. this is the current object, and therefore is always exists.

You are using a char - as this is a primitive is also cannot be null. Change that to the object wrapper and check that for null

class Cell {

    int column;
    int row;
    Character letter;

    public Cell(int column, int row, Character letter) {
        this.column = column;
        this.row = row;
        this.letter = letter;
    }

    public boolean isEmpty() {
        return letter == null;
    }
}

Another note is that the superclass constructor is always called by default, there is no reason to call super().

Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
  • Thank you. Is there any option if I'm not allowed to change letter's type ? – ampc Apr 04 '13 at 01:02
  • 1
    From [this](http://stackoverflow.com/questions/9909333/whats-the-default-value-of-char) the default value of `char` if `\u0000`, which isn't a printable character - you _could_ test for that. Not overly robust however. – Boris the Spider Apr 04 '13 at 01:05
  • I'm guessing you don't want to change from `char` to `Character` because you use primitive `char`s already in other code? You should be able to transparently switch to `Character` in the `Cell` class due to [autoboxing](http://docs.oracle.com/javase/7/docs/technotes/guides/language/autoboxing.html). So constructing a new `Cell` (like `Cell c = new Cell(2, 3, 'x');`) still works if `Cell` uses `Character` instead of `char`, and you can use the `null` check. Otherwise, you'll have to choose some default value for `letter` to check, probably `\u0000` as bmorris591 suggested. – ajp15243 Apr 04 '13 at 01:28
0

If the instance of an object exists, then it cannot be null! (as the comment of Code-Guru says). However, what you are trying to do is to check if the letter attribute of your object is (or is not) null.

Just as a suggestion, instead of using char as the type, use Character, which is the class that encapsulates the char type.

Your class then will may look like this:

class Cell {
    int column;
    int row;
    Character letter;

    public Cell(int column, int row, Character letter) {
        super();
        this.column = column;
        this.row = row;
        this.letter = letter; // This is an object, not a primitive type 
    }

    public boolean isEmpty() {
        if (letter==null) 
            return true;
        else 
            return false;
    }
}
Barranka
  • 20,547
  • 13
  • 65
  • 83
0

this cannot be null because this is your instance of a Cell. Without changing char to Character:

class Cell {
    int column;
    int row;
    char letter;

    public Cell(int column, int row, char letter) {
        super();
        this.column = column;
        this.row = row;
        this.letter = letter;
    }

    public boolean isEmpty() {
        return letter == 0;
    }
}
ACV
  • 9,964
  • 5
  • 76
  • 81