Note that there are some explanatory texts on larger screens.

plurals
  1. POJava - Get/set methods receiving and returning "null"
    primarykey
    data
    text
    <p>I'm a beginner in Java. I'm trying, for training purpose, to build myself a chess game application. Within my class Case, that will be used to instanciate all the 64 cases of my board, I write get/set methods to find if there's a Piece occupant in the instances of the case.</p> <p>I read that returning "null" is a bad practice, so I throw an exception instead to signify that the case is free. But, I wonder how to set the occupant's pointer to "null"; can I simply push "null" as a parameter when I will call this method?</p> <p>Also, could taking/returning "null" be an acceptable/good practice?</p> <pre><code>public Piece getOccupant(){ if (this.occupant == null) throw new IllegalArgumentException(this.occupant + " is Empty"); return this.occupant; } public void setOccupant(Piece newOccupant){ this.occupant = newOccupant; } </code></pre> <p>Thanks!</p> <p>[Update]</p> <p>Thanks to all of your for your comments, ideas, corrections and recommendations. Here is the updated version of my code for this part, and I feel satisfied with it, as it served its purpose (increase my understanding thru practice).</p> <pre><code>/* * Modifiers of Occupant */ /** * Used to find if a Piece is located in this Cell * @return a Piece reference to the occupant. Will send a * null pointer if cell is empty */ public Piece getOccupant(){ return this.occupant; } /** * Used to set a new occupant in the Cell. * @param newOccupant is a reference to a Piece instance, * and should be set to null if the cell is emptied, or using * the method clear(). */ public void setOccupant(Piece newOccupant){ this.occupant = newOccupant; } /** * Used to verify if a Cell is empty of any occupant * @return true if cell is empty. */ public boolean isEmpty(){ if(this.occupant == null) return true; return false; } /** * Free the cell of any occupant, if any were */ public void clear(){ this.occupant = null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload