Java Null Pointer Check

In this post we will see a simple method to put a Java null pointer check in place with minimal effort.

NullPointerException unarguably is the worst nightmare for a Java developer. You can see this exception on even some of the reputed sites on the internet.

Basically you get this exception when you are trying to access some method over an object reference which is pointing to null.

e.g. The below statement will throw NullPointerException if str is null:

str.toUpperCase();

Whenever you are accessing some method over an object reference, it is always a good practice to make sure it is not null, else you can expect a NullPointerException in some situations.

Most of the time developers ignore putting a check in place, they either feel very confident that the object won’t be null, or perhaps they just feel lazy!!

In this tutorial we will show a simple way to perform a null pointer check using a single line of code.

Here is source:

NullPointerCheck.java

package com.quicklyjava;

public class NullPointerCheck {

	/**
	 * @param args
	 */

	public static void main(String[] args) {
		TheObject t = new TheObject();
		Integer i = t.getInt();
		String p;
		p = (i != null) ? i.toString() : "Just prevented a Null Pointer";
		System.out.println(p);

	}

}

TheObject.java:

Around The World Flights - Promo Code WORLD15
package com.quicklyjava;

public class TheObject {

	/**
	 * @param args
	 */
	Integer n;

	public TheObject() {
		this.n = null;
	}

	public Integer getInt() {
		// TODO Auto-generated method stub
		return n;
	}
}

Here is the output of the program:

Java Null Pointer Check

So, the null pointer check boils down to this code:

p=(i!=null)?i.toString():"i is null";

We use ternary operator here to check whether the reference i is null or not. For those who are not aware about ternary operators – it is a compact if else block. In this case it translates as below:

if(i!=null)
	p = i.toString();
else
	p = "i is null";

Thus we have compressed at least 4 lines of code into 1.

To conclude, its always a good practice to have java null pointer checks in order to avoid catastrophic situations, and as we saw in this tutorial, we can effectively use ternary operator to achieve this.

You can download the eclipse project of the code explained in this tutorial:
NullPointerCheck (4KB)

Please consider following our blog (via Email, Twitter, Facebook and/or Google+ – See the right sidebar) for more interesting posts.

10 responses to “Java Null Pointer Check”

  1. Nikos Maravitsas

    Hi,

    Great blog! Is there an email address I could contact you in private?

    1. Haider

      Wow, your post makes mine look feeble. More power to you!

  2. lista de emails

    i tend to agree with you.

    1. Nicole

      At last some rationality in our little debate.

  3. Dog Yoga

    I enjoy reading write-up. Hope i can discover a lot more articles like this one. Thanks for posting.

  4. thefightbetting

    http://www.quicklyjava.com is excellent, good work!
    Regards
    Price Harrison odds

  5. Evelyn Jeanne Shaw

    Just wish to say your article is as surprising. The clearness in your post is just great and i can assume you are an expert on this subject. Well with your permission let me to grab your feed to keep updated with forthcoming post. Thanks a million and please carry on the rewarding work.

    1. Miracle

      I thought I’d have to read a book for a discovery like this!

  6. GyncPrieric

    I had been more than happy to seek out this web-site.I wanted to I appreciate the effort because of this fantastic read!! I surely enjoying each and every little bit of it and I have you bookmarked to have a look at new stuff you blog post.

    Am I Able To just say what a relief to find someone who truly knows what theyre talking about on the net.

Leave a Reply