45758

How to check if object is null in Java?

Question:

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

Answer1:

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

Answer2:

If the instance of an object <em>exists</em>, 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; } }

Answer3:

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

Recommend

  • Script to export layer coordinates to excel
  • Running the Python Code on Hadoop Failed
  • saving and retrieving key-value pairs in an sqlite database android
  • How to parse Response xml in JMeter and send the result as dynamic parameters to another http reques
  • Why processBuilder in java hangs after 5 mins?
  • List using with references, changes behavior when used as a member
  • Insert statement not working using execute(array()) of PDO Extension
  • Android NDK refer to external libraries in JNI
  • Unsupported ciphersuite TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
  • Implementation of timeout in LDAP
  • GTK3 + OpenGL for windows: multiple GtkGLArea (ie. OpenGL contexts)
  • PHP: Need to close STDIN in order to read STDOUT?
  • How to load dynamic images in custom ListView
  • Annotate objects in a queryset with next and previous object ids
  • Problems using phonegap / cordova file plugin part 2 - synchronicity
  • How to define something in JavaScript [closed]
  • How to make 100% div height between header and footer?
  • r - input value by user to dataframe via shiny
  • Amazon Elastick BeanStalk error: Failed to create the AWS Elastic Beanstalk application version
  • how to run ejabberd with Erlang on Heroku?
  • Terminal run dalvikvm with am.jar
  • How to specify generic type when the type is only known at runtime?
  • Will this work on all screen sizes?
  • How to decleare char *const argv[] in swift [duplicate]
  • PHP/MySQL Special Characters aren't displayed properly [duplicate]
  • Functions by reference or by variable, which to use when?
  • Why my AngularJS async test in Jasmine 1.3.x is not working?
  • Grails - How to implement a foreign key relationship not using an id column?
  • Codeigniniter insert data through models and controller
  • Capture SIGFPE from SIMD instruction
  • Cross compile glibc for arm, got undefined reference to some unwind functions
  • Using Service Component Runtime
  • Angular FormGroup won't update it's value immediately after patchValue or setValue
  • How to use FirstOrDefault inside Include
  • How do I use TagLib-Sharp to write custom (PRIV) ID3 frames?
  • WPF custom control and direct content support
  • CAS 4 - Not able to retrieve the LDAP groups after successful authentication
  • Spring Boot not autowiring @Repository
  • JavaScript RegExp Replace
  • ARKit code issue {unknown error -1=ffffffffffffffff error: Task failed with exit 1}