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;
    }
}
2
  • 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?) Commented Apr 4, 2013 at 0:47
  • Derek Greer, at 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. Commented Apr 4, 2013 at 1:07

3 Answers 3

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().

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Is there any option if I'm not allowed to change letter's type ?
From this the default value of char if \u0000, which isn't a printable character - you could test for that. Not overly robust however.
I'm guessing you don't want to change from char to Character because you use primitive chars already in other code? You should be able to transparently switch to Character in the Cell class due to autoboxing. 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.
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;
    }
}

Comments

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.