Java - Get / set methods to get and return "null"

I am new to Java. I am trying to create a chess game application for training purposes. In my Case class, which will be used to initiate all 64 cases of my board, I write get / set methods to find out if there is a blacksmith person in cases.

I read that returning "null" is bad practice, so instead of throwing an exception, I make an exception to show that the case is free. But I am wondering how to set the pointer to "null"; can i just click "null" as a parameter when i call this method?

Also, can accept / return โ€œnullโ€ an acceptable / good practice?

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

Thanks!

[Update]

Thanks to everyone for your comments, ideas, corrections and recommendations. Here is an updated version of my code for this part, and I feel satisfaction from it as it served its purpose (increase my understanding through practice).

 /* * 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; } 
+8
java set null get
source share
6 answers

Unused space on board is not exclusive. Its normal and will always be true for most board members. You should not throw exceptions here; exceptions should only be thrown for an unexpected event, which means a significant problem with what you are trying to do.

Of course, you can pass null to the setter (except for a primitive type like int / long).

It might be better to add some convenient methods, the isEmpty method for your Space class:

 public boolean isEmpty(){ if (this.occupant == null) return true; return false; } 

and also possibly a clear method

 public void clear() { this.occupant = null; } 

thus, you donโ€™t need to check the getter result for invalidity, and you do not need to pass a null value for installation - this has the additional advantages of being easily verifiable and creates an API that makes sense to your Space class.

+10
source share

If you want to disable null values, you must do this using the setter method:

 public void setOccupant(Piece occupant) { if (occupant == null) throw new NullPointerException("occupant"); this.occupant = occupant; } 

Note that some people prefer to throw an IllegalArgumentException. In any case, the fact is that the "crash is fast" as soon as someone sets a forbidden value.

Having said all this, a chessboard can certainly have empty positions, so null resolution seems more reasonable.

I suggest you read Effective Java, Second Edition by Josh Bloch.

+6
source share

Where did you read this recommendation? In my opinion, there is nothing wrong with returning null , provided that null conveys some useful information and does not indicate a serious error condition. In this case, it is perfectly normal if the chess cell does not contain a piece, and I would definitely expect getOccupant() return null in this case.

+3
source share

If the caller is aware of the returned NULL values, it is nice to return the NULL values โ€‹โ€‹on the call.

+1
source share

Instead of returning null or throwing an exception, you should create a class of "Empty", "No", "Void", something like that, that you would assign everything empty to empty.

+1
source share

a small sentence there is no need for a block, you can simplify your code by simply returning the output of the expression

 public boolean isEmpty(){ return this.occupant == null } 
0
source share

All Articles